"Knowledge has to be improved, challenged, and increased constantly, or it vanishes."

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.

12 Comments

Comments have been disabled for this content.