Incomplete Leo

Something from my heart

ASP.NET 2.0 and Url Rewrite – Advance

ASP.NET 2.0 support Url Rewrite :


Several people have asked why the built-in URL Mapper in ASP.NET 2.0 doesn’t support regular expressions. There were actually a few reasons for this — one of the big ones being that just about the time we were about to consider adding it my team started also working on IIS7. We realized that a full-featured version would want/need to take advantage of some of the new features in IIS7 as well as the support all content types (in particular — images and directories). So we postponed making it feature rich until a future version.

It’s a shame given that it only takes 20 lines to support a regex based URL rewriter – and that IIS7 won’t even be launched until Longhorn Server, sometime in 2007 (and even then, most IT depts will wait for SP1 before deploying)
The Whidbey URL mapper was written mid-2003, it’s a shame in the intervening 2 years 20 lines of code couldn’t be written and tested.


It only takes 20 lines to support a basic regex based URL rewriter. But if you want it to be secure and scalable it takes a lot more work.
Specifically, you need to do a lot of work to make sure that you aren’t succeptible to denial of service attacks where remote users send urls that cause computationally intensive regex queries to spin your CPUs.
The 20 line sample you have in your website doesn’t deal with this, and probably doesn’t need to if it is only used on the occasional server. But Microsoft can’t get away with that if we install it on 4 million servers (that would be too big a target).

Finally, the solution :

01. Is UrlRewritingNet.UrlRewrite for free?

Yes it is! It can be freely shared and distributed, changed, developed and used for any commercial cases. But we take no warranty for erroless using the software.

02. What is the special about UrlRewritingNet?

It is a rewrite-module tuned for ASP.NET 2.0, and offers support for

  • Themes and Masterpages
  • Regular Expressions
  • good Postback-Urls
  • Cookieless Sessions
  • Runs in Shared-Hosting or Medium-Trust enviroments
  • OutputCache is supported
  • Redirects possible, even to other Domains

The setup is also quick 🙂

03. Is there a Version for ASP.NET 1.1?

No, and is not planned. You can use the original version from Fabrice Marguerie.

04. Are Rewrites possible without a Fileextension?

No, this is not possible without changing the IIS configuration because these requests are directly not handled by ASP.NET 2.0
Yes if you can change the IIS Configuration, take a look the UrlRewritingNet documentation.


Tháng Một 19, 2007 Posted by | ASP.NET 2.0 Related | Bạn nghĩ gì về bài viết này?

ASP.NET 2.0 and Url Rewrite – Simple

Simple approach :

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, which is really the point of an Url Mapper. ScottGlu at his blog, explains the reason why the ASP.NET team didn’t implement this featur, basically because 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 :

public void Rewrite_BeginRequest(object sender, System.EventArgs args) {

     string strPath = HttpContext.Current.Request.Url.AbsolutePath;

     UrlRedirection oPR = new UrlRedirection();

     string strURL = strPath;

     string strRewrite = oPR.GetMatchingRewrite(strPath);

     if (!String.IsNullOrEmpty(strRewrite)) {

          strURL = strRewrite;

     } else {

          strURL = strPath;


     HttpContext.Current.RewritePath("~" + strURL);

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:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>


     <rule name="Category Page">




     <rule name="Item Page">





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

public string GetMatchingRewrite(string URL)  {
     string strRtrn = "";

     System.Text.RegularExpressions.Regex oReg;

     foreach (RedirectRule oRule in Rules) {

          Reg = new Regex(oRule.URL);
          Match oMatch = oReg.Match(URL);

          if (oMatch.Success)  {
               strRtrn = oReg.Replace(URL, oRule.Rewrite);

     return strRtrn;

 Sample here

Tháng Một 19, 2007 Posted by | ASP.NET 2.0 Related | Bạn nghĩ gì về bài viết này?