Routing with web forms application

Microsoft .NET Framework 3.5 SP1 introduced a routing engine to the ASP.NET runtime. The routing engine can decouple the URL in an incoming HTTP request from the physical Web Form that responds to the request, allowing you to build friendly URLs for your Web applications.

Let's start make our web application work with routing, first of all you need to do some changes in your global.asax file, add reference in your application for

// Add reference in your application for DLL

// Add following line to your global.asax file
<%@ Import Namespace="System.Web.Routing" %>

Create a method in your global.asax file (we will write the route here latter)

public void RegisterRoutes(System.Web.Routing.RouteCollection routes)

Now call this method in your Application_Start method in global.asax file

void Application_Start(object sender, EventArgs e) 

That's it you need to do, now your application can accept URLs like the following

  1. http://server/application/CarDetail/1234
  2. http://server/application/Report/CityName/2009
  3. http://server/application/Expenses?page=5

Now time to add the routing for these urls in your RegisterRoutes method created above

// For 1 URL
routes.MapPageRoute("", "CarDetail/{carid}", "~/CarDetail.aspx")  

// For 2 URL
routes.MapPageRoute("CityReport", "Report/{cityname}/{year}", "~/Report.aspx");  

// if you want to pass the default city
routes.MapPageRoute("CityReport", "Report/{cityname}/{year}", "~/Report.aspx", true
    , new RouteValueDictionary { 
             { "cityname", 'defulatCityName' }

// if you want to pass the default city and year
routes.MapPageRoute("CityReport", "Report/{cityname}/{year}", "~/Report.aspx", true
    , new RouteValueDictionary { 
             { "cityname", 'defulatCityName' }
            ,{ "year",  Datetime.Now.Year.ToString()}

// For 3 URL: no need to write anything for query string in routing
routes.MapPageRoute("", "Expenses", "~/Expenses.aspx");  

If you are using IIS 7.5 then you will need to add following lines in your web.config otherwise you will get error, page not found

      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" 
           type="System.Web.Routing.UrlRoutingModule" preCondition="" />
Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • routing
By Ali Adravi On 05 Jan, 13  Viewed: 2,613

Other blogs you may like

Readonly textbox postback issues and solutions

In many cases we need to use read only text box so user cannot edit the value but readonly textbox will lost its value after postback. Let’s say you have a read only text box to set date by using ajax calendar so after post back date will be lost, other case might be you are setting some value in... By Ali Adravi   On 24 Apr 2013  Viewed: 4,302

Call code behind method from JavaScript in

There are ways to call a web service method JavaScript, for more detail you can see [how to retrieve data from database using JavaScript in][1], but is there any way to call a normal method from JavaScript? And the answer is No; show how we can call a code behind method from JavaScript,... By Jonathan King   On 08 Apr 2013  Viewed: 12,506

Gridview paginated data with search and sort functionality in

Most of the times we need to use GridView control to show tabular data in our application. We simply write procedure to search the records and bind them with GridView and show 10 to 20 records per page. Have you ever thought that why you are fetching all the records and show only 10 to 20... By Ali Adravi   On 16 Feb 2013  Viewed: 8,569

ASP.Net 4.5 new feature Model Binding

A nice feature with ASP.Net is the model binding, it reduced our code and effort to bind our well know controls like GridView, DataList, Repeater etc. So let’s see how we can bind our old controls in new and easy way. Let’s see how we bind the our grid before 4.5 1. We write the code to get... By Mike .Net   On 17 Jan 2013  Viewed: 3,217

Upload multiple image in multiple size with progress bar in

In there is not control to select multiple files and upload them once with progress bar, so we will use a small third party DLL to achieve this functionality. We will use Flajaxian FileUploader, you can download it from [][1] We will create three different images... By Hamden   On 12 Jul 2012  Viewed: 6,567