zoukankan      html  css  js  c++  java
  • UrlRewrite(URL重写)--ASP.NET中的实现

    概述

    今天看了下URL重写的实现,主要看的是MS 的URL Rewrite。

    URL重写的优点有:更友好的URL,支持老版本的URL

    URL重写的缺点有:最主要的缺点是性能低下,因为如果要支持无后缀的URL(但更多的情况是我们要支持这种方式)就必须在IIS中配置所有的URL(包括js,css,image)都要转发到aspnet_isapi中,解决方法可以参见 慎用url重写;还有一个性能问题是,根据源代码,在匹配url时,用正则表达式尝试匹配每一个规则,直至有一个匹配成功,或都匹配不成功才结束。那么那些不需要重写的URL,就要将所有的正则表达式都要执行一次,可以在进入匹配之前先做一个判断,排除掉一些情况

    1 配置 web.config

    1.1 下载 MS的URLRewrite

    地址是:http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

    1.2 配置自定义section的声明节点

      <configSections>
        <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
      </configSections>

    1.3 配置自定义section的内容

    用UrlRewrite在完成配置后,主要的工作就是写配置url映射的正则表达式了。对于正则式不清楚的人,可以看看这个正则表达式入门及备忘

      <RewriterConfig>
        <Rules>
          <RewriterRule>
            <LookFor>~/pick/?</LookFor>
            <SendTo><![CDATA[~/pick.aspx]]></SendTo>
          </RewriterRule>
          <RewriterRule>
            <LookFor>~/pick/(d+)</LookFor>
            <SendTo><![CDATA[~/pick.aspx?page=$1]]></SendTo>
          </RewriterRule>
          <RewriterRule>
            <LookFor>~/(w+)/p/(d+).html</LookFor>
            <SendTo><![CDATA[~/BlogDetails.aspx?blogwriter=$1&blogid=$2]]></SendTo>
          </RewriterRule>
          <RewriterRule>
            <LookFor>~/Product/(w{4}).html</LookFor>
            <SendTo>~/Product.aspx?id=$1</SendTo>
          </RewriterRule>
        </Rules>
      </RewriterConfig>

    1.4 配置httpmodules或httphandlers

    在如果是旧的IIS 6.0,在<system.web>节点下配置,httpmodules和httphandlers只需要一个就好,用于拦截所有请求

        <httpModules>
            <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
        </httpModules>
        
        <!--<httpHandlers>
            <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
        </httpHandlers>-->

    在新的IIS7.0中, 在<system.webServer>节点下配置, modules 和handlers同样只需要一个

      <system.webServer>
        <modules>
          <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
        </modules>
        <!--<handlers>
          <add verb="*" path="*.*" type="URLRewriter.RewriterFactoryHandler, URLRewriter" name="URLRewriter" />
        </handlers>-->
      </system.webServer>

    1.5 配置IIS

    在发布到IIS后,如果访问路径出错,需要做一下扩展名的映射。额,我用的IIS7.0是没有报错,直接就可访问了。

    2 代码分析

    UrlRewrite的源代码十分的简单,实际上就是实现了一个IHttpModule的接口来拦截所有的请求URL。然后,将请求的URL用正则表达式与每一个匹配,直到有一个匹配成功,则将原有的url(假的)根据正则表达式转成实际的url。如果都匹配不成功,则按原有的路径处理请求

    主要代码是

    protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
            {
                // log information to the Trace object.
                app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
    
                // get the configuration rules
                RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;
    
                // iterate through each rule...
                for(int i = 0; i < rules.Count; i++)
                {
                    // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                    string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
    
                    // Create a regex (note that IgnoreCase is set...)
                    Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);
    
                    // See if a match is found
                    if (re.IsMatch(requestedPath))
                    {
                        // match found - do any replacement needed
                        string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));
    
                        // log rewriting information to the Trace object
                        app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
    
                        // Rewrite the URL
                        RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                        break;        // exit the for loop
                    }
                }
    
                // Log information to the Trace object
                app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
            }
    View Code

    看了这个源代码,还学到的一个东西就是在web.config中自定义节点,然后实现一下IConfigurationSectionHandler 接口,将section的内容转成对象,在程序中使用,主要代码有:

            public static RewriterConfiguration GetConfig()
            {
                if (HttpContext.Current.Cache["RewriterConfig"] == null)
                    HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationManager.GetSection("RewriterConfig"));
                
                return (RewriterConfiguration) HttpContext.Current.Cache["RewriterConfig"];
            }
    
    
        /// <summary>
        /// Deserializes the markup in Web.config into an instance of the <see cref="RewriterConfiguration"/> class.
        /// </summary>
        public class RewriterConfigSerializerSectionHandler : IConfigurationSectionHandler 
        {
            /// <summary>
            /// Creates an instance of the <see cref="RewriterConfiguration"/> class. : IConfigurationSectionHandler 
            /// </summary>
            /// <remarks>Uses XML Serialization to deserialize the XML in the Web.config file into an
            /// <see cref="RewriterConfiguration"/> instance.</remarks>
            /// <returns>An instance of the <see cref="RewriterConfiguration"/> class.</returns>
            public object Create(object parent, object configContext, System.Xml.XmlNode section) 
            {
                // Create an instance of XmlSerializer based on the RewriterConfiguration type...
                XmlSerializer ser = new XmlSerializer(typeof(RewriterConfiguration));
    
                // Return the Deserialized object from the Web.config XML
                return ser.Deserialize(new XmlNodeReader(section));
            }
    
        }
    View Code

    3 最后

    最后就是,UrlReWrite虽然好用,但是根据这源代码,性能问题是个大头,但平常自己的小网站用用还是没问题的,如果请求量大了,也知道这里UrlRewrite这里有问题,可以用httphander的方式只拦截特定的url,也可以用IIS filter去搞(没搞过,但应该能用,毕竟是在IIS端拦截大部分的请求,而不是将求放到扩展程序中去)。关于IIS filter可以参考这个文章  在 ASP.NET 中执行 URL 重写

  • 相关阅读:
    Codeforces714C【映射】
    Codeforces712C【贪心】
    Codeforces712B【= =】
    lightoj1259 【素数预处理】
    Codeforces482B【线段树构造】
    51nod 1348【next_permutation】
    hdoj5289【RMQ+二分】【未完待续】
    hdoj5875【二分+RMQ】
    RMQ算法
    AtCoder Regular Contest 061 DSnuke's Coloring
  • 原文地址:https://www.cnblogs.com/dataadapter/p/4649802.html
Copyright © 2011-2022 走看看