ASP.NET 2.0: Rewriting URL Paths just got a whole lot easier

29. June 2005

Rewriting URL paths in ASP.NET 2.0 is referred to as URL Mapping. Instead of putting some code in your Application_BeginRequest method, you can now just put a few lines in your Web.Config file and like magic you can rewrite those URL paths with ease.

With URL Mapping you can turn a not so freindly URL like ~/Blog/Post/11/12/2005/145.aspx into ~/TheCoolestPost.aspx

Example Web.Config File:


<?xml version="1.0" ?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.web>


    <urlMappings enabled="true">
      <add
          url="~/Category.aspx"
          mappedUrl="~/Default.aspx?category=default" />
      <add
          url="~/Autos.aspx"
          mappedUrl="~/Default.aspx?category=autos" />
      <add
          url="~/Games.aspx"
          mappedUrl="~/Default.aspx?category=games" />
      <add
          url="~/Health.aspx"
          mappedUrl="~/Default.aspx?category=health" />
      <add
          url="~/News.aspx"
          mappedUrl="~/Default.aspx?category=news" />
    </urlMappings>


  </system.web>
</configuration>

Since the Web.Config file is an XML document, you could add functionality to the admin section of your app to add/edit/delete custom URL Mappings. This is definately something I'm going to keep in mind for future ASP.NET 2.0 app I design/build.

kudos to the ASP.NET team. This is just one of the many new features added to ASP.NET 2.0. And they couldn't have made it any easier to use!

URL Mapping in ASP.NET 2.0: http://beta.asp.net/QUICKSTART/aspnet/doc/navigation/urlmapping.aspx

If you want to see how to rewrite url paths with ASP.NET 1.x go here: http://edsid.com/blog/articles/160.aspx

General ,



Comments

7/5/2005 8:45:00 AM #
Check out the following link for my own implementation of http://asp.net" target="_blank">ASP.NET 2.0 style URL Mapping to use in your ASP.NET 1.1 application.

pietschsoft.com/Blog/archive/2005/07/04/717.aspx" rel="nofollow">pietschsoft.com/Blog/archive/2005/07/04/717.aspx">pietschsoft.com/Blog/archive/2005/07/04/717.aspx" rel="nofollow">pietschsoft.com/Blog/archive/2005/07/04/717.aspx
10/14/2005 11:42:00 PM #
Well, that's nice an clean but not very practical.  In almost every case, you will have dynamic URL's that need to be matched and replaced using Regular Expressions.  I can't believe that http://Microsoft.com" target="_blank">Microsoft would pitch this as an idea without having built in the features to handle RegEx replacements.  Now I have to go hunt down how to do this in http://asp.net" target="_blank">ASP.NET 2.0 because I can no longer use the 1.0 implementation because of Schema differences.

Am I missing something here, or is this a really big miss on Microsofts part?
10/15/2005 2:10:00 AM #
Yeah, I'm not sure why http://Microsoft.com" target="_blank">Microsoft didn't bake in regular expression support with this feature.

I haven't tested my http://asp.net" target="_blank">ASP.NET 1.1 implementation of URL Mapping with 2.0, but I imagine it'll work with minimal reworking.
11/12/2005 9:41:00 PM #
I wrote my own implementation of URL Mapping for http://asp.net" target="_blank">ASP.NET 2.0 that supports Regular Expressions. Located Here: pietschsoft.com/Blog/archive/2005/11/12/762.aspx" rel="nofollow">pietschsoft.com/Blog/archive/2005/11/12/762.aspx">pietschsoft.com/Blog/archive/2005/11/12/762.aspx" rel="nofollow">pietschsoft.com/Blog/archive/2005/11/12/762.aspx
Comments are closed