Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

URL rewriting using ASP.NET routing

Rate me:
Please Sign up or sign in to vote.
4.83/5 (23 votes)
7 Jul 2009CPOL2 min read 134.3K   6.4K   70   27
MVC style search engine friendly URLs using ASP.NET routing, which comes as an enhancement in SP1 of ASP.NET 3.5.

Introduction

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 http://mysite.com/add_new_user.

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 (url_routing.zip). 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.

XML
<add assembly="System.Web.Routing, Version=3.5.0.0, 
     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.

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

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

C#
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:

C#
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).

C#
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:

License

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


Written By
Apdynamics Inc
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIIS settings for URLREWRITING Pin
neil@euclid3-Nov-10 1:49
neil@euclid3-Nov-10 1:49 
AnswerRe: IIS settings for URLREWRITING Pin
neil@euclid9-Nov-10 1:22
neil@euclid9-Nov-10 1:22 
QuestionProblem with master page and user control Pin
jinal.mehta31-May-10 20:32
jinal.mehta31-May-10 20:32 
AnswerRe: Problem with master page and user control Pin
AnuragBajpai1-Jun-10 4:24
AnuragBajpai1-Jun-10 4:24 
QuestionHow to combine UrlRoutingModule with AjaxControlToolkit Pin
usuphan14-Dec-09 5:23
usuphan14-Dec-09 5:23 
GeneralBest article on ASP.NET Routing Pin
nomi198427-Oct-09 3:13
nomi198427-Oct-09 3:13 
GeneralA big Thanks Pin
Bjorn van der Neut21-Aug-09 10:40
Bjorn van der Neut21-Aug-09 10:40 
QuestionInteresting Pin
stixoffire8-Jul-09 5:17
stixoffire8-Jul-09 5:17 
AnswerRe: Interesting Pin
rippo13-Jul-09 21:07
rippo13-Jul-09 21:07 
AnswerRe: Interesting Pin
AnuragBajpai13-Jul-09 22:04
AnuragBajpai13-Jul-09 22:04 
GeneralRe: Interesting Pin
Abbas_Riazi15-Jul-09 18:00
professionalAbbas_Riazi15-Jul-09 18:00 
GeneralRe: Interesting Pin
AnuragBajpai16-Jul-09 3:21
AnuragBajpai16-Jul-09 3:21 
GeneralRe: Interesting Pin
suis10-Sep-10 6:30
suis10-Sep-10 6:30 
GeneralRe: Interesting Pin
mullapudipraveen3-Jan-11 19:43
mullapudipraveen3-Jan-11 19:43 
Hi,

I have one Question for System.Web.Routing.

Is there any chance to Route Forms under Specific Floder instead of Single Form.

For Example : I have Pageone.aspx,PageTwo.aspx,PageThree.aspx under Pages Floder.
how can i do routing for Pages Floder Instead of induvidual Pages.

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

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