ASP.NET 4.0 WebForms Routing – Part I

Posted: August 9, 2009 in ASP.NET
Tags: ,

Those who have been working in ASP.NET MVC are already aware of the ASP.NET Routing feature introduced with .NET 3.5 SP1.But I have seen many who has developed a perception that ASP.NET Routing is something that can be used with only ASP.NET MVC.This is not the case.ASP.NET Routing comes under a separate namespace (System.Web.Routing) and assembly (System.Web.Routing.dll).This can be used with WebForms applications as well.But this required some custom coding to develop your RouteHandler as shown in the post below:

In ASP.NET 4.0, added capabilities are provided to make routing fully complete in order to work with WebForms.This is what I will be discussing in this post today.I will use simple webpage with 2 hyperlinks to demonstrate this feature.

Step1: Add a reference to System.Web.Routing.dll to the ASP.NET Web Application project.

Step2: Add the UrlRoutingModule in the web.config as shown below.This HttpModule intercepts the incoming request and routes the control to appropriate handler.

  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule"/>

Step3:We have to define the Routes and populate the RouteTable.RouteTable class exposes a static property Routes which is a collection of Route objects.This RouteTable is referenced by the ASP.NET Routing Engine to resolve the route values from the URL.The RouteTable needs to be populated at the Application_Start event of Global.asax.cs as shown below:


protected void Application_Start(object sender, EventArgs e)

private void RegisterRoutes()
   RouteTable.Routes.Add("Products",new Route("{product}",new PageRouteHandler("~/Default.aspx")));

Here Routes.Add method expects two parameters:


  • A string identifying the name of the Route.In this case I have given “Products”
  • An instance of the RouteBase class.This is the base class and I have passed an instance of the Route class which is an implementation of RouteBase.
    • Route class takes url pattern and an instance of IRouteHandler as argument.Here I have passed an instance of PageRouteHandler which is an implementation of IRouteHandler interface specifically designed to handle of ASP.NET Webforms.
      • PageRouteHandler takes the relative path of the WebForm as input.

As a demo I have used a simple page with two hyperlinks as shown below:

<form id="form1" runat="server">
<h2> Routing Demo</h2>
            <asp:HyperLink  runat="server" NavigateUrl="/Books" Text="Books"></asp:HyperLink>
            <asp:HyperLink  runat="server" NavigateUrl="/CDDVD" Text="CD and DVD"></asp:HyperLink>

In the Page_Load event I have retrieved the value passed as Route in the url as shown below:


protected void Page_Load(object sender, EventArgs e)
           string product = RouteData.Values["product"] as string;
           if (product != null)
               Response.Write("Product::" + product);



RouteData is a new property that is exposed by the Page class and contains the route names and values.

This is a very rudimentary working sample.In the next post we will take a more detailed look into what PageRouteHandler class is doing.

  1. […] the rest here: ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal SHARETHIS.addEntry({ title: "ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal", […]

  2. […] the original here:  ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal SHARETHIS.addEntry({ title: "ASP.NET 4.0 WebForms Routing – Part I « Sankarsan's Journal", […]

  3. […] This post was Twitted by Dave_Bush […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s