Sharker Khaleed Mahmud's Blog

ASP.NET, MVC, BLEND, Silverlight, AJAX and JQuery

  • Me

    Sharker Khaleed Mahmud
  • Visit my recent blog

ASP.NET Url Routing tutorial in simple ways

Posted by Sharker Khaleed Mahmud | shamrat231 on November 4, 2009

The main purpose of this article is to show you how easy it is to implement url routing. I will not be using any database or complex scenario to explain the concept as this is my first article. Things that will cover in this article are given below.

• Managed Code url routing
• Hard-Coded url routing
• Markup url routing
• Unnamed url routing
• Catching routing parameters

First of all lets create a simple project in ASP.NET.

Once you click on ok, a sample project will be created. Now lets add 5 web form pages(.aspx) in a folder name UrlDemo. Each page is using the builtin master page (Site.Master). Afterwards, create a class UrlRoute.cs in a new folder. Give the folder name App_Code.


Now add a simple static method in UrlRoute.cs using the System.Web.Routing class

public static void RegisterRoutes(RouteCollection routes)

RouteCollection just provides a collection of route for ASP.NET routing. So lets add some routes to this collection.

MapPageRoute is a helper method. In the first parameter you give a name of the route. For simplicity, lets assing name like AboutRoute, SearchRoute etc. The second parameter is the parameter that will be shown on the browser url. You can also pass parameter if needed. The over loading method for MapPageRoute also allows you to add constraint and default parameter. See the below example.


Here we have created two routes. The first one has name “MainRoute”. The second parameter is the pattern that will be shown to the user. It may contain multiple place holder(s) if needed. I will talk about it a bit later. The last parameter contains the physical location of the file that we are routing.

Ok, now we will call this RegisterRoutes method from Application_Start


Great we are almost there. Now I am going to show you how we can use this routing from code level. First, open default.aspx web form that is provided by default. Add some hyperlink(s)


Now on default.aspx.cs, i will create a sample virtual path (VirtualPathData) for MainRoute.


Hit F5 and put a debug on the last line. At runtime, you will be able to see the url pattern at debuging virtual path as shown below. So we can conclude uptil now its working.


So far we have created a sample virtual path from code level. The next route is AboutRoute. In this one i will show you on how can we assign path directly. Look at the picture below. Remember, we have already added the route path in RegisterRoutes method.


By the way, try adding information  in all of the pages like the way given below. So you will know that the routing is actually working.


Ok, so we have cover the basics. Now I will register more routes by adding new routes.


Here, we have created a CustomerRoute with a second parameter containing the pattern. In this case, it’s a simple pattern with a {year} place holder and  to-catch other {*paramerters} here. In the third parameter we are giving the physical location of the file. In the fifth parameter we assign default value for the routes. In the last parameter we define the constrains for the default value for the defined route. Now, in default.aspx.cs, we will call this route and assign it to CustomerRoute navigation url.


Hit F5 and see for yourselves. Similarly we can assign markup variables in aspx page.


The above code is an example of markup language. The hyperlink with id ProfileRoute, in this case we did not assign any route name. As a result, it will automatically take the unnamed route in routecollection.


In this case unnamed route here is profile. Ok, the last thing left is to catch the routing values. So in the next section, at code level, we will try to catch the parameter that we passed. For this case, we will use Customer.aspx.cs


Guess I will finish here. Any comments welcome. Next week I will talk about Silverlight Prism and MVVM.

Sharker Khaleed Mahmud

ASP.NET Routing


One Response to “ASP.NET Url Routing tutorial in simple ways”

  1. Thanks for your great post. It is working perfectly and it is suitable for any number of parameters in url.

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

%d bloggers like this: