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/Prof
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.asp
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/II
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/pos









