Click here to Skip to main content
11,412,516 members (73,785 online)
Click here to Skip to main content

URL rewriting using ASP.NET routing

, 7 Jul 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
MVC style search engine friendly URLs using ASP.NET routing, which comes as an enhancement in SP1 of ASP.NET 3.5.


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:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Apdynamics Inc
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 4 Pin
vsanju at 27-May-13 1:25
membervsanju27-May-13 1:25 
QuestionURL rewriting using ASP.NET routing Pin
vsanju at 27-May-13 1:24
membervsanju27-May-13 1:24 
GeneralMy vote of 5 Pin
talhartk at 17-May-13 1:04
membertalhartk17-May-13 1:04 
QuestionUrl Rewritting Question Pin
DNYANESHWAR KAWATHE at 18-Mar-13 22:20
memberDNYANESHWAR KAWATHE18-Mar-13 22:20 
AnswerRe: Url Rewritting Question Pin
memberDNYANESHWAR KAWATHE7-May-13 4:35 
Questionhow to get parameter value on destination page. Pin at 3-Apr-12 18:29
membermaneeshkatiyar09@gmail.com3-Apr-12 18:29 
SuggestionMore discription Pin
atulkath28 at 2-Apr-12 11:22
memberatulkath282-Apr-12 11:22 
QuestionYour solution don't work on IIS 6 with Wild Card Entry Pin
Pushkarr at 3-Feb-12 20:40
memberPushkarr3-Feb-12 20:40 
AnswerRe: Your solution don't work on IIS 6 with Wild Card Entry Pin
feng_woalang at 10-Dec-12 18:06
memberfeng_woalang10-Dec-12 18:06 
Member 8238496 at 23-Dec-11 2:46
memberMember 823849623-Dec-11 2:46 
BugProblem with image and css paths Pin
Kike Vesga at 4-Oct-11 20:56
memberKike Vesga4-Oct-11 20:56 
GeneralProblem With UrlRoting Pin
Shrivastva at 18-Feb-11 3:21
memberShrivastva18-Feb-11 3:21 
QuestionIIS settings for URLREWRITING Pin
neil@euclid at 3-Nov-10 2:49
memberneil@euclid3-Nov-10 2:49 
AnswerRe: IIS settings for URLREWRITING Pin
neil@euclid at 9-Nov-10 2:22
memberneil@euclid9-Nov-10 2:22 
QuestionProblem with master page and user control Pin
jinal.mehta at 31-May-10 21:32
memberjinal.mehta31-May-10 21:32 
AnswerRe: Problem with master page and user control Pin
bajpai.anurag at 1-Jun-10 5:24
memberbajpai.anurag1-Jun-10 5:24 
QuestionHow to combine UrlRoutingModule with AjaxControlToolkit Pin
usuphan at 14-Dec-09 6:23
memberusuphan14-Dec-09 6:23 
GeneralBest article on ASP.NET Routing Pin
nomi1984 at 27-Oct-09 4:13
membernomi198427-Oct-09 4:13 
GeneralA big Thanks Pin
Bjorn van der Neut at 21-Aug-09 11:40
memberBjorn van der Neut21-Aug-09 11:40 
QuestionInteresting Pin
stixoffire at 8-Jul-09 6:17
memberstixoffire8-Jul-09 6:17 
AnswerRe: Interesting Pin
rippo at 13-Jul-09 22:07
memberrippo13-Jul-09 22:07 
AnswerRe: Interesting Pin
bajpai.anurag at 13-Jul-09 23:04
memberbajpai.anurag13-Jul-09 23:04 
For Dynamic mapping:
In the sample code, I am adding a new user in RegisterUSer.aspx page, the list of added users is showing on the same page.( with the url that consists their names) ie if i add a user dynamically (say stixoffire) then his details can be accessed on

For Postback:
It will not have any problem with postback, but their may have problem with path of css or images.( need to be careful).

Anurag Bajpai

GeneralRe: Interesting Pin
A. Riazi at 15-Jul-09 19:00
memberA. Riazi15-Jul-09 19:00 
GeneralRe: Interesting Pin
bajpai.anurag at 16-Jul-09 4:21
memberbajpai.anurag16-Jul-09 4:21 
GeneralRe: Interesting Pin
suis at 10-Sep-10 7:30
membersuis10-Sep-10 7:30 
GeneralRe: Interesting Pin
mullapudipraveen at 3-Jan-11 20:43
membermullapudipraveen3-Jan-11 20:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.1 | Last Updated 7 Jul 2009
Article Copyright 2009 by AnuragVajpayee
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid