zoukankan      html  css  js  c++  java
  • URLRewriter修改使你更容易理解重写原理

    在上一篇,我感觉有的地方不是太好理解,所以我在这里修改了一下,更通俗易懂了,为了让更容易理解。
    web.config中configSections中添加这个这个节点,根据这个节点  ,你可以看出创建自定义的节点是什么样子的,
    <sectionGroup name="RewriterConfig">
          <section name="Rule" type="WebApplication1.RewriterConfigSerializerSectionHandler,WebApplication1"/>
        </sectionGroup>
    
      <RewriterConfig>
        <Rule>
            <RewriterRule>
            <LookFor>~/About-(\d+).html</LookFor>
            <SendTo>~\About.aspx?id=$1</SendTo>
          </RewriterRule>
          <RewriterRule>
            <LookFor>~/About-(\d+)-(\d+).html</LookFor>
            <SendTo>~\About.aspx?id=$1&t=$2</SendTo>
          </RewriterRule>
        </Rule>
      </RewriterConfig>

    这一点他们必须相同。

    同样的你必须创建一个RewriterConfigSerializerSectionHandler去实IConfigurationSectionHandler接口
        public class RewriterConfigSerializerSectionHandler : IConfigurationSectionHandler
        {
            public object Create(object parent, object configContext, XmlNode section)
            {
                //// Create an instance of XmlSerializer based on the RewriterConfiguration type...
                //XmlSerializer ser = new XmlSerializer(typeof(RewriterConfigss));
    
                //// Return the Deserialized object from the Web.config XML
                //return ser.Deserialize(new XmlNodeReader(section));
                RewriterRule para = null;
                IList<RewriterRule> list = new List<RewriterRule>();
                for (int i = 0; i < section.ChildNodes.Count; i++)
                {
                    para = new RewriterRule();
                    XmlNode xm = section.ChildNodes[i];
                    para.SendTo=  xm.SelectSingleNode("SendTo").InnerText;
                    para.LookFor = xm.SelectSingleNode("LookFor").InnerText;
                    list.Add(para);
                }
                return list;
    
            }
        }
    上面注释是url重写的源码,在这里大家可以看出我修改了。这个样子更容易被理解
        public class RewriterConfigss
        {  
           
          //public RewriterRuleCollection Rules { get; set; }
            /// <summary>  
            /// 该方法从web.config中读取规则集合,并使用了Cache以避免频繁IO操作  
            /// </summary>   
            /// <returns></returns> 
          public static IList<RewriterRule> GetConfig()
            {
              
                //使用缓存    
                if (HttpContext.Current.Cache["RewriterRule"] == null)
                    HttpContext.Current.Cache.Insert("RewriterRule", ConfigurationManager.GetSection("RewriterConfig/Rule"));
                return ((IList<RewriterRule>)HttpContext.Current.Cache["RewriterRule"]);
            }
        }
    }
    ConfigurationManager.GetSection("RewriterConfig/Rule")就这点要注意的是,这么写是为了他直接获得就是里面那些RewriterRule节点集合
    就这些了,其他地方和上一遍中的代码一样。
    

      

  • 相关阅读:
    ASCII对应码表-键值(完整版)
    node.js中使用路由方法
    关于vscode自动跳转回车的解决方法(关闭vscode自动保存功能;可能和其他插件有冲突)
    js中 !==和 !=的区别是什么
    spring 请求参数和路径变量
    PowerShell因为在此系统中禁止执行脚本解决方法
    SQL server 2008数据库的备份与还原(亲测,效果良好)注意采用单用户模式呀
    webpack-dev-server提示css模块解析失败,但已经装了css-loader
    webpack集成vue单文件模式的很多坑(研究了1个星期)
    npm全局模块卸载及默认安装目录修改方法
  • 原文地址:https://www.cnblogs.com/waters/p/URLRewriter.html
Copyright © 2011-2022 走看看