Friendly URL Rewriting in ASP.NET

Freshness Warning
This post is more than 3 years old. Please bear in mind its age when reading.

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
 

 

 



 

 


 

I'm running the 39th BMW Berlin Marathon on 30th September 2012 for the British Lung Foundation, who are currently funding research on the prevention of lung damage in COPD and many other areas related to lung disease.

I'm participating with my sister Claire Kewney and, on behalf of the charity, would appreciate even the smallest donation. My own JustGiving page is here, our team page is here.

if you're in the UK, you can also donate using your mobile phone by texting NKEW82 £5 (or any amount) to 70070. Your donation will be appreciated!

Comments

  1. discount internet pharmacy http://www.fda-approved-pharmacy.com/products/cymbalta.htm college of pharmacy in taiwan
Your Comment
Your Name
E-mail Address (This won't be published)
Website URL

You can manage your Kewney.com account by logging in. [ Log On ]