ASP.NET 4.0 URL Routing Tutorial

It is a common mis-perception that URL Routing is  exclusive to MVC, with .NET 3.5 SP1 and above URL Routing can be also used with ASP.NET Web Forms. In ASP.NET 4.0 URL Routing is fully integrated and so straightforward and powerful to use it should be preferred over URL Rewriting whenever possible.

Routing allows for SEO friendly URLs such as aspnet101.com/category/routing/ to be used instead of a URL such as aspnet101.com/category.aspx?categoryid=8 .

First you will need to register the Routes in a subroutine and then add them in the Application_Start() event:

void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"articles-category",  //Name of the Route (can be anyuthting)
"articles/{category}", // URL with parameters
"~/category.aspx"); //Page which will handles and process the request
}

void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}

In the above code, the Route named articles-category will handle incoming requests for URLs which match the articles/{category} route definition. This route definition uses “articles/” as a literal string in the URL and then takes the subsequent URL text for the category parameter.

For example, the URL articles/routing will direct to the page category.aspx and pass “routing” as the value for the category parameter. There is a lot of flexibility in mapping parameters to the URL, please refer to Default Values, Constraints, Multiple Parameters for more details.

The parameters from the URL are passed to the aspx page in key-value dictionary pairs which can be accessed from Page.RouteData.Values. So in the above example the category value can be read into a variable using the below code:

string category = Page.RouteData.Values["category"] as string;

This code would typically be placed in the Page_Load() event of the page.

In addition to accessing the parameters programatically, they can also be read by the SQLDataSource control using the <asp:routeparameter> control:

<asp:sqldatasource id="ds1" runat="server"
connectionstring="<%$ ConnectionStrings:Main %>"
selectcommand="select title, text from Articles where category=@cat">
<selectparameters>
<asp:routeparameter name="category" RouteKey="cat" />
</selectparameters>
</asp:sqldatasource>

Generate URLs Using ASP.NET Routing

A major difference between Routing and URL rewriting is that Routing also allows for the generation of outgoing URLs served on the site. In the above example if we wanted to show users a URL for the ado category of articles, then we could generate a URL by using the below code:

string url = Page.GetRouteUrl("articles-category",
  new { category = "ado" });

This code uses the articles-category Route to generate a url string, this could be used on the page or passed to the Response.Redirect method to redirect to another page. Whilst the last use is valid, if the user needs to be redirected to another page using Routing it is more efficient to use the Response.RedirectToRoute method:

Response.RedirectToRoute("articles-category",
  new { category = "ado" });

2 thoughts on “ASP.NET 4.0 URL Routing Tutorial

  1. Pingback: ASP.NET MVC Routing Tutorial – Part I | ASP.NET 101

  2. Pingback: New Features in ASP.NET 4.0 | ASP.NET 101

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>