URLRewriter源碼分析

來源:互聯網
上載者:User
public class RewriterConfigSerializerSectionHandler : IConfigurationSectionHandler{     #region IConfigurationSectionHandler 成員      XmlSerializer他是可以把xml還原序列化成對象,也可以把對象序列化一個xml,但是要求是這兩個裡面屬性都帶一直(元素名和預設命名空間)      大概是這個意思,要是還不明白的話,去csdn上查詢吧,http://msdn.microsoft.com/zh-cn/library/system.xml.serialization.xmlserializer(v=VS.80).aspx  public object Create(object parent, object configContext, XmlNode section)  {       聲明一個類型為RewriterConfigss序列化對象      XmlSerializer ser = new XmlSerializer(typeof(RewriterConfigss));      講section(xml)還原序列化成RewriterConfigss這個對象      return ser.Deserialize(new XmlNodeReader(section));  }#endregion}  另外就是這個類了,web請求流程明白的人們,應該都知道,請求先到httpmodule在到httphandle,然後在返回到httpmodule。在handle裡面就是伺服器對使用者請求進行處理了,產生頁面源碼啊,等等,所以,我們要在他到達httphandle之前進行修改url。重寫httpmodule要繼承IHttpModule介面,同樣要實現裡面的init方法了。public abstract class BaseModuleRewriter:IHttpModule{  public virtual void Dispose()  {  }  public virtual void Init(HttpApplication context)  {        / /在請求剛開始的時候         context.BeginRequest += new EventHandler(BaseModuleRewriter_BeginRequest);  }  protected virtual void BaseModuleRewriter_BeginRequest(object sender, EventArgs e)  {         HttpApplication app = (HttpApplication)sender;         Rewrite(app);  }  /// <summary> /// 地址修正抽象函數 /// </summary> /// <param name="app"></param> ///         protected abstract void Rewrite(HttpApplication app);}  下面這個是繼承了上面的那個類,所以就不用我說了,當上面的走進那個委託的時候就進子類了這個Rewrite方法了。我們在web。config中配置的匹配原則有多個RewriterRule類似:<RewriterConfig>  <Rules>    <RewriterRule>      <LookFor>~/About-(\d+).html</LookFor>      <SendTo>~\About.aspx?id=$1</SendTo></RewriterRule><RewriterRule>      <LookFor>~/About-(\d+).html</LookFor>      <SendTo>~\About.aspx?id=$1</SendTo>    </RewriterRule>  </Rules></RewriterConfig>,所以,我們為了方便讀取管理,建立了RewriterRuleCollection類public class ModuleRewriter : BaseModuleRewriter{  protected override void Rewrite(HttpApplication app)  {         //擷取規則集合     RewriterRuleCollection rules = RewriterConfigss.GetConfig().Rules;     for (int i = 0; i < rules.Count; i++)     {       string lookFor = rules[i].LookFor;       Regex reg = new Regex(lookFor, RegexOptions.IgnoreCase);      if (reg.IsMatch("~"+app.Request.RawUrl))       {       //擷取目的URL       string sendToUrl = reg.Replace("~"+ app.Request.RawUrl, rules[i].SendTo);       //地址修正 (發送到httphandle進行處理)      app.Context.RewritePath(sendToUrl);       break;       }     }  }}  在Rewriter源碼中這個類叫做RewriterConfiguration,我感覺改成這個更容易讓人理解,從節點中你就看,你就明白了。但是在源碼中你可以看到類中加了一個[XmlRoot("RewriterConfig")] 屬性,這個意思就是把這個名字加到這個類的xml根部,在這裡就是說的ConfigurationManager.GetSection("RewriterConfigss")),他返回的值是<Rules>  <RewriterRule>    <LookFor>~/About-(\d+).html</LookFor>     <SendTo>~\About.aspx?id=$1</SendTo></RewriterRule><RewriterRule>    <LookFor>~/About-(\d+).html</LookFor>    <SendTo>~\About.aspx?id=$1</SendTo></RewriterRule></Rules>你看看缺少什麼,對,缺少我們的根,(RewriterConfiguration)HttpContext.Current.Cache["RewriterConfig"],這句話,就是為什麼這個
源碼中的這個類RewriterConfiguration可以的原因了。

  

  

[Serializable()]public class RewriterConfigss{     表示RewriterConfigss的屬性    public RewriterRuleCollection Rules { get; set; }  /// <summary>   /// 該方法從web.config中讀取規則集合,並使用了Cache以避免頻繁IO操作   /// </summary>   /// <returns></returns>   public static RewriterConfigss GetConfig()  {    //使用緩衝     if (HttpContext.Current.Cache["RewriterConfig"] == null)    HttpContext.Current.Cache.Insert("RewriterConfig", ConfigurationManager.GetSection("RewriterConfigss"));    return (RewriterConfigss)HttpContext.Current.Cache["RewriterConfig"];  }}[Serializable()]public class RewriterRuleCollection : CollectionBase{  /// <summary>   /// 向集合中添加新規則   /// </summary>   /// <param name="r">RewriterRule對象</param>   public virtual void Add(RewriterRule r)   {     this.InnerList.Add(r);   }   /// <summary>   /// 擷取或設定項  /// </summary>   public RewriterRule this[int index]   {     get { return (RewriterRule)this.InnerList[index];}     set { this.InnerList[index] = value; }   }}

  

這是個人的學習程度,如果其中有什麼錯誤的地方請指出來,謝謝! 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.