UrlRewrite(URL重寫)--ASP.NET中的實現

概述

今天看了下URL重寫的實現,主要看的是MS 的URL Rewrite。css

URL重寫的優勢有:更友好的URL,支持老版本的URLhtml

URL重寫的缺點有:最主要的缺點是性能低下,由於若是要支持無後綴的URL(但更多的狀況是咱們要支持這種方式)就必須在IIS中配置全部的URL(包括js,css,image)都要轉發到aspnet_isapi中,解決方法能夠參見 慎用url重寫;還有一個性能問題是,根據源代碼,在匹配url時,用正則表達式嘗試匹配每個規則,直至有一個匹配成功,或都匹配不成功才結束。那麼那些不須要重寫的URL,就要將全部的正則表達式都要執行一次,能夠在進入匹配以前先作一個判斷,排除掉一些狀況web

1 配置 web.config

1.1 下載 MS的URLRewrite正則表達式

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

1.2 配置自定義section的聲明節點app

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

1.3 配置自定義section的內容ide

用UrlRewrite在完成配置後,主要的工做就是寫配置url映射的正則表達式了。對於正則式不清楚的人,能夠看看這個正則表達式入門及備忘oop

  <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 重寫

相關文章
相關標籤/搜索