Friendly URL Rewriting in ASP.NET

This article demonstrates a how to create friendly URLs for user content in ASP.net applications. The examples below are in C#. Convert them to VB if that's your language of choice.

Background

Sites like Twitter provide users with a direct link to their content in the format of "http://twitter.com/username".

Supplying a simple URL to enable users to share their content is integral to the success of any social networking site and can be beneficial in many other applications.

The following two example solutions demonstrate how to achieve this using 1) ASP.net alone and 2) an ISAPI filter with slightly less overhead.

So let's start with an example. Currently, your users visit their profiles by visiting http://www.site.com/Profile.aspx?u=myusername. You want to make life simpler for them remove the ugly query string values and references to the ASPX page (e.g. http://www.site.com/users/myusername)

There are two ways this can be achieved:

Solution #1

Place the following code in the Global.asax within your project. You will then have to tell IIS that ASP.net will handle all requests (not just aspx requests).

 ' Global.asax Variable
private System.Text.RegularExpressions.Regex rxUserURI;
' Add to Application Start event
rxUserURI = new Regex("^/users/(?<MyUserName>.+)$", RegexOptions.Compiled);
' Add to Application BeginRequest event
System.Text.RegularExpressions.Match match = rxUserURI.Match(Request.Url.AbsolutePath);
if (match.Success) {
  Response.Redirect(string.Format("http://www.site.com/Profile.aspx?u={0}", match.Groups["MyUserName"].Value));
}

You'll notice I set it up to match users in the users directory so it doesn't call confusion if you've got other subdirectories.
ie. you don't want www.mysite.com/articles to forward to www.mysite.com/Profile.aspx?u=articles

This method pretty much mirrors Apache's ModRewrite 

Solution #2

The other option would be to use an ISAPI filter such as Ionic ISAPI at http://www.codeplex.com/IIRF

I suggest using this method for production as it is far more lightweight than using .NET to handle every type of request. It's a great, free and highly configurable.

How to Redirect All Requests Through ASP.net

 If you're wondering how to tell IIS to redirect all requests through the ASP.net engine, the following link explains how to do this. You'll have to setup the .Net ISAPI filter as the default filter for all file types.

http://tim-stanley.com/post/How-To-Enable-HTM-Server-Side-Include-Parsing-in-IIS.aspx
 

 

 



 

 


 

 

#1 Pam on 4.14.2009 at 12:08 PM

Great article, Nick. Should I ever need to do friendly URL rewriting for ASP.net, now I know how to do it!

#2 Will on 4.28.2009 at 10:55 PM

I get the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

on the line:

Dim match As System.Text.RegularExpressions.Match = rxUserURI.Match(Request.Url.AbsolutePath)

#3 Nick on 4.28.2009 at 10:57 PM

Hi Will,

I definitely recommend using option #2, the ISAPI filter.

http://www.codeplex.com/IIRF

Really easy to set up on IIS6. Let me know if you want a sample config file...

Nick

#4 Amit Habu on 9.01.2009 at 12:00 PM

Hello,Please can u provide me an example of url rewriting,since i tried 3 different examples of url rewriting in asp.net.All are working on my local m/c perfectly,but not on server.Can u provide me an simple example of url rewriting.Also i want rewriting of url through response.redirect but not an hyperlink...

#5 Deanna18Burgess on 7.17.2010 at 2:18 AM

Don't you acknowledge that it is correct time to get the <a href="http://bestfinance-blog.com/topics/personal-loans">personal loans</a>, which would help you.

Leave a Comment