URL rewriting with RegEx for ASP. NET 2.0 (using regular expressions in ASP. net2.0 to create URL rewriting)

Source: Internet
Author: 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 wocould 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 enabled 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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.