Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



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:

[code:html]
<?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>
[/code]

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

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General
Posted by crpietschmann on Wednesday, June 29, 2005 2:43 PM
Permalink | Comments (4) | Post RSSRSS comment feed

Related posts

Comments

Christopher Pietschmann, MCSD, MCAD

Tuesday, July 05, 2005 6:45 AM

Christopher Pietschmann, MCSD, MCAD

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

Ryan Smith

Friday, October 14, 2005 9:42 PM

Ryan Smith

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?

Christopher Pietschmann, MCSD, MCAD

Saturday, October 15, 2005 12:10 AM

Christopher Pietschmann, MCSD, MCAD

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.

Christopher Pietschmann, MCSD, MCAD

Saturday, November 12, 2005 7:41 PM

Christopher Pietschmann, MCSD, MCAD

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

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann