Click here to Skip to main content

URL rewriting using ASP.NET routing


Sometimes we need to have search engine friendly URLs for our application. In ASP.NET (before ASP.NET MVC/3.5 SP1), we had to use some third party tools like URL rewriter, which work fine but require wild card mapping in IIS and have issues with IIS7.

ASP.NET MVC has introduced the System.Web.Routing assembly for rewriting URLs; now, it is included in ASP.NET 3.5 SP1. Using the routing assembly, you can have search engine friendly URLs like

Now we can use the System.Web.Routing assembly in our ASP.NET website to create such URLs. (Please see the download file from above.)

Using the code

You can find the complete source code from the download provided above ( Unzip the file and open using Visual Studio 2008 (open the website wizard), and you will need to browse to the folder "url_rout" (that should contain the web.config file).

First of all, you have to add a reference to System.Web.Routing in your application, or you can add the following line in the web.config (under the assemblies section). You must have ASP.NET 3.5 SP1 installed.

<add assembly="System.Web.Routing, Version=, 
     Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Now, we need to create different routes in global.asax. In the first route "Add_New_User", we don't have any parameters; but in the second route "User/{name}", we have a parameter called name.

void Application_Start(object sender, EventArgs e) 
    // Code that runs on application startup
private static void RegisterRoutes()
           new System.Web.Routing.Route("Add_New_User",
               new RouteHandler("~/RegisterUser.aspx")));
           new System.Web.Routing.Route("User/{name}",
               new UserRouteHandler("~/DisplayUser.aspx")));

UserRouteHandler and RouteHandler are classes that implement IRouteHandler, as shown below:

public class UserRouteHandler : IRouteHandler
    public UserRouteHandler(string virtualPath)
        _virtualPath = virtualPath;

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
        var display = BuildManager.CreateInstanceFromVirtualPath(
                      _virtualPath, typeof(Page)) as IUserDisplay;
        display.UniqueName = 
          requestContext.RouteData.Values["name"] as string;
        return display;

     string _virtualPath;

You will notice that IUserDisplay is an interface which implements IHttpHandler:

public interface IUserDisplay : IHttpHandler
    string UniqueName { get; set; }

IUserDisplay has to be implemented by the page to be rendered (DisplayUser.aspx, in this case) as shown below, so that the unique identifier (UniqueName, in the case) or parameter will be found in the page, which can be used to show the appropriate data (like the query string).

public partial class DisplayUser : Page, IUserDisplay
    public string UniqueName { get; set; }

    protected void Page_Load(object sender, EventArgs e)
        if (UniqueName != null && !UniqueName.Equals(""))
        //do some coding

Points of interest

I have found that all samples and tutorials on this topic are for either ASP.NET MVC applications or web projects, and nothing if I want to create a normal ASP.NET website (however, there are not much differences between them). Here are some useful links for routing:

Web03 | 2.8.160204.4 | Advertise | Privacy
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service