Routing in ASP.NET 4 Web Forms

 

ASP.NET version 4 has built-in support for URL routing.

Why Routing is Required

Traditionally, in an ASP.NET application that does not use routing, any request will map to a particular file that serves the request. With database-driven applications where the data is fetched from the database based on query string values, the URL is difficult to read and difficult for users to remember. Additionally, nowadays the search engines are looking more into URLs to determine the relevancy of the content; so URLs such as http://servername/product.aspx?id=1 will be ranked lower than something like http://servername/products/Ovens. So for these reasons it is very important to provide meaningful URLs.

About Prior versions

There is a good article published by Scottgu that describes URL rewriting features available in prior versions of ASP.Net. You can read the article from the following link. http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

What is available in ASP.Net version 4

With ASP.Net 4, routing comes inside the framework. There is a new namespace introduced System.Web.Routing to handle routing of URLs. The System.Web.Routing namespace contains classes that implement URL rewriting inside ASP.Net web forms. The highlights of routing features available in ASP.Net 4 are follows

1. Routing enables developers to use URLs that do not have to map to a certain file

2. Developers can provide more descriptive/meaningful URLs that can be easily understandable by users and search engines

3. Routing in ASP.Net 4 provides full class support by allowing users to define any custom route to a web form page

Demo

For demonstrating URL Routing in ASP.Net 4.0, I have made a simple page, products.aspx. The page load event handler for the project page is as follows

 

protected void Page_Load(object sender, EventArgs e)
{
string category = Request.QueryString["category"];
if (category == "fridge")
{
Response.Write("all fridges");
}
else if (category == "tv")
{
Response.Write("all TVS");
}
else if (category == "oven")
{
Response.Write("all ovens");
}
else
{
Response.Write("miscellanious or unknown option");
}
}

When you browse products.aspx?category=fridge will give you the response all fridges. In a typical application, the data will be queried from a database and show accordingly. For the demo purpose, I made it all simple, concentrating only on routing. Now I am going to implement routing for products.aspx page show that end users can use meaningful URLs.

Define the Routes

You need to define the routing in your global.asax file. I have added a public static function for registering routes and called this from the Application_start event handler. You may add any number of routes in the Application_Start event handler.

protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("RouteToProducts",
"Products/{categoryname}",
"~/products.aspx");
}

In the above code, you define the routes by using the function routes.MapPageRoute. The three parameters passed are friendly name for the routes, URL format and handler page respectively.

Now all requests in the Products/Categoryname format will be redirected to the products.aspx file, The only thing we need to so is that instead of retrieving the category from query string, read it from the RouteData values for the current page.

string category = Page.RouteData.Values["categoryname"].ToString();

Now the page load for products.aspx will look like follows

protected void Page_Load(object sender, EventArgs e)
{
string category = Page.RouteData.Values["categoryname"].ToString();
if (category == "fridge")
{
Response.Write("all fridges");
}
else if (category == "tv")
{
Response.Write("all TVS");
}
else if (category == "oven")
{
Response.Write("all ovens");
}
else
{
Response.Write("miscellanious or unknown option");
}
}

It is possible to define any number of route parameters; for example a typical route can be Products/{categoryname}/{model}/{productname} which can map URLs such as Products/Laptops/Thoshiba/sattelite

 

Hope you enjoyed the article.

Thanks.

Published Wednesday, June 23, 2010 12:11 PM by sreejukg
Filed under: ,

Comments

# Routing in ASP.net 4.0 web forms

Wednesday, June 23, 2010 4:28 AM by WebDevVote.com

You are voted (great) - Trackback from WebDevVote.com

# re: Routing in ASP.net 4.0 web forms

Wednesday, June 23, 2010 6:02 PM by Ali Taki

Cool I was looking for this and you presented it as easy as it can get, thanks

# re: Routing in ASP.net 4.0 web forms

Wednesday, June 23, 2010 8:54 PM by Joseph Wittman

always good to see helpful code thanks.

# re: Routing in ASP.net 4.0 web forms

Thursday, June 24, 2010 4:12 AM by denegrine

gun movit clouds increases

# re: Routing in ASP.net 4.0 web forms

Thursday, June 24, 2010 8:56 PM by clevonpapa

list small weather ozone

# re: Routing in ASP.NET 4 Web Forms

Monday, July 12, 2010 1:07 PM by Huseyin DEMIRTAS

thank u for explaining this cool technology in plain English

# re: Routing in ASP.NET 4 Web Forms

Monday, July 19, 2010 3:39 AM by no_matter

This feature came in Net3.5 SP1.

# re: Routing in ASP.NET 4 Web Forms

Monday, August 16, 2010 11:27 AM by Jack

Useful, i like it!

# re: Routing in ASP.NET 4 Web Forms

Tuesday, October 12, 2010 1:56 PM by Charandeep Singh

Thank you so much. I guess to use this you have to have .NET 4 running with Intergrated Pipeline?

# re: Routing in ASP.NET 4 Web Forms

Tuesday, November 09, 2010 5:46 AM by Sudhakar

Ur Explanation is very Nice.....

I Hope U to Post Such Articles...keep Posting

very nice......

# re: Routing in ASP.NET 4 Web Forms

Thursday, November 25, 2010 12:36 PM by weblogs.asp.net

g684jhdkff r <a href='jazzpins.com/weblogs.asp.net'>weblogs.asp.net</a> h cvq [url=jazzpins.com/weblogs.asp.net]weblogs.asp.net[/url] jsr cracdxjvp jazzpins.com/weblogs.asp.net ssigizfgn

# re: Routing in ASP.NET 4 Web Forms

Thursday, November 25, 2010 4:20 PM by weblogs.asp.net

gh78dkdufgujd7 yweneiqwr <a href='usecafmg.org/weblogs.asp.net'>weblogs.asp.net</a> klxo zaueov [url=usecafmg.org/weblogs.asp.net]weblogs.asp.net[/url] ryrsjf wnrhw usecafmg.org/weblogs.asp.net zqeiynafx

# re: Routing in ASP.NET 4 Web Forms

Wednesday, December 15, 2010 5:40 AM by anyoffice.net

sd6g79sdfgsd6f7gsd776sfg rvfc <a href='http://www.anyoffice.net/'>anyoffice.net</a> mwibp ekmp [url=http://www.anyoffice.net/]anyoffice.net[/url] yitfw ueop http://www.anyoffice.net/ noqvzhmck

# re: Routing in ASP.NET 4 Web Forms

Monday, January 03, 2011 1:02 PM by usha

very easy to understand. thanks

# re: Routing in ASP.NET 4 Web Forms

Friday, February 11, 2011 11:29 AM by Pradeep

Very nice introduction.

I need to route subdomain to an user name, in ASP.NET 4 Web Forms.

Ex:

ypradeep23.wordpress.com should be reouted to wordpress.com/default.aspx

- Pradeep Y.

# re: Routing in ASP.NET 4 Web Forms

Wednesday, May 25, 2011 12:35 PM by Vikash Anil

Thanks. Do we have complete domo in asp.net 4.0 Web Forms with C#. I want to rewrite my url http://smcs.in?id=10 to smcs.in/.../blue-template

Thanks.

Vikash(http://learnonlinewithme.com)

Leave a Comment

(required) 
(required) 
(optional)
(required)