Url Rewriting with Regex for ASP.NET 2.0(在asp.net2.0中使用正規運算式建立URL重寫)

來源:互聯網
上載者:User

A new feature in Asp.Net 2.0 is it's built-in url rewriting support. When i looked into this new feature i found out it lacked regular expressions support, wich is really the point of an Url Mapper. ScottGlu at his blog, explains the reason why the Asp.Net team didn't implemented this feature. Basically they realized that a full featured version would want to take advantage of the next IIS 7.0 new features, specially the support for all content-types (images and directories).

Anyway, it's really simple to implement a Url Rewriting Module with Regex support in Asp.Net. I wrote a quick and simple HttpModule for this. The whole magic is done within a few lines within the HttpModule:

 1 public void Rewrite_BeginRequest(object sender, System.EventArgs args) { 2      string strPath = HttpContext.Current.Request.Url.AbsolutePath; 3      UrlRedirection oPR = new UrlRedirection(); 4      string strURL = strPath; 5      string strRewrite = oPR.GetMatchingRewrite(strPath); 6       if (!String.IsNullOrEmpty(strRewrite)) { 7           strURL = strRewrite; 8      } else { 9           strURL = strPath;10      }11      HttpContext.Current.RewritePath("~" + strURL);12 }

The code is self explanatory. When a request that is processed by the Asp.Net engine, the module checks an xml for a regex match. I've seen many Url Rewriting engines that uses Web.config to store the matching rules but i prefer using an additional xml file. The rewriting rules file look like the following:

 1 <?xml version="1.0" encoding="utf-8" standalone="yes"?> 2 <urlrewrites> 3      <rule name="Category Page"> 4           <url>/([a-zA-Z][\w-]{1,149})\.aspx</url> 5           <rewrite>/Default.aspx?Category=$1</rewrite> 6      </rule> 7      <rule name="Item Page"> 8           <url>/([a-zA-Z][\w-]{1,149})/([a-zA-Z][\w-]{1,149})\.aspx</url> 9           <rewrite>/Default.aspx?Category=$1&amp;Item=$2</rewrite>10      </rule>11 </urlrewrites>

The rule matching routine, wich is implemented in the GetMatchingRewrite() method is quite simple and lightweighted:

 1 public string GetMatchingRewrite(string URL)  { 2      string strRtrn = ""; 3  4      System.Text.RegularExpressions.Regex oReg; 5  6      foreach (RedirectRule oRule in Rules) { 7  8           Reg = new Regex(oRule.URL); 9           Match oMatch = oReg.Match(URL);10 11           if (oMatch.Success)  {12                strRtrn = oReg.Replace(URL, oRule.Rewrite);13           }14 15      }16      return strRtrn;17 }

I have uploaded a sample project that uses this rewriting engine. The HttpModule and it's helper classes are inside the App_Code folder. I hope you find this code useful, if you have any questions just leave a comment in this entry. Happy coding!

FROM DEVEL.oping.net
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.