Chris Pietschmann

husband, father, hacker, entrepreneur, futurist, innovator, autodidact

NAVIGATION - SEARCH

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

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

blog comments powered by Disqus