We have been looking at all the parts that make a sample ASP.Net MVC application. Previously we have discussed the database schema of our application as well as implementation of the Repository Pattern with filters on that schema. If you haven’t been following this series of posts you might want to read parts 1 and 2 before continuing. Url Routing has become a very common these days. In fact, at least among the websites I visit, it has become more common than not routing urls.

What Is Url Routing?

Url Routing is part of the ASP.Net framework, both MVC and WebForms, that lets you easily rewrite and map your urls to make them look nicer and be more serach engine friendly. An example would be yourdomain.com/Products/Show/3. The routing map would take yourdomain.com/Product/Show/3 and map it too it’s real url of yourdomain.com/default.aspx?controller=Product&action=Show&id=3. Like I said, this is a very common technique for websites and web applications so I won’t spend anymore time explaining what is and will jump right into showing you how it works in ASP.Net MVC.

Default Url Routing

Url Routing is enabled by default when you create a new MVC application from the Visual Studio template. Visual Studio creates the necessary sections in your Web.config file and defines the default route table in Global.asax file. In the Web.config file there are 4 sections related to Url Routing. They are

system.web.httpModules

system.web.httpHandlers

system.webserver.modules

system.webserver.handlers

These sections make Url Routing work so make sure you leave them alone. In Global.asax the default route table code looks like this:

1:publicclass GlobalApplication : System.Web.HttpApplication

2: {

3:publicstaticvoid RegisterRoutes(RouteCollection routes)

4: {

5: routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

6:// MapRoute takes the following parameters, in order:

7:// (1) Route name

8:// (2) URL with parameters

9:// (3) Parameter defaults

10: routes.MapRoute("Default"

11: , "{controller}/{action}/{id}"

12: , new { controller = "Home", action = "Index", id = "" });

13: }

14:

15:publicvoid Application_Start()

16: {

17: RegisterRoutes(RouteTable.Routes);

18: }

19: }

When our application is first loaded the Application_Start method is called which calls our RegisterRoutes method which sets up the route table. The RegisterRoutes method does a few things that we are going to look at. First off, it defines a route that will be ignored. Any request that contains a .axd file will be ignored which means the url will be left alone and will be processed as it is. The scond part is the MapRoute method that takes three arguments. The first argument is the route name, in this case the route is named Default. The second argument is the route definition. This definition will split an incoming request into 3 parts, the controller, the action, and id. The third arugment is a default route. In case there is not a route in the request (i.e. the request is the root of the domain, yourdomain.com/) then this route will be used. What does this mean? If your application receives a request like yourdomain.com/Product/Show/45, the route table will tell you application to use the Product controller, the Show action within the Product controller, and use 45 as the id. Simple right? That is pretty much all there is to Url Routing. The default map route will probably work for most application needs although if you require a different map route you can define your own custom route.

Defining A Custom MapRoute

A trival example of a custom route would be as follows.

1: routes.MapRoute("Default"

2: , "{id}/{action}/{controller}"

3: , new { controller = "Home", action = "Index", id = "" });

In this route all I did was change the order so the id comes first and the controller is last so, using the sample url from above, our request would look like yourdomain.com/45/Show/Product.

In the next article I’ll take a look at controllers and how they manage the flow of your application.

3 Responses to “Working With ASP.Net MVC Part 3 – Url Routing”

Thanks for a good article. The custom map route is definitely useful. One problem I notice is the the nice-looking URL must be in a specific site.com/controller/action/id format, or some order of that sequence using custom map routes. What if your site has urls with varying parameters or paths? It seems like a rigid solution.

Goku

Said on
January 11, 2009 :

A fellow developer and I are having issues with the ASP.NET 3.5 MVC Preview 5 and the display of images (.jpg/.gif) inside a view (using tags), plus the loading .css or .js files. Can someone from this site provide us some direction regarding something we may have missed either in the Web.config, or in the VS2008 IDE that is blocking these items from loading, or a means of determining in a log where the failure is occuring? We have tried both relative and absolute path-ing to no avail. We are using Windows integrated security, and the files are set to be Read and Executed in IIS 5/6 by the group using the application. Thanks for any help you can give us. We really appreciate your time and efforts in helping us.

Harendra chauhan

Said on
January 27, 2009 :

I have ASP .net MVC website created on my machine ,it is runnig fine on my machine ,all links are working but when I hosted is on IIS 5.1 that time home page is assible from other pc on network but all its Links(url routing) is not working,Images are not getting loaded , css is not working.. Plz provide the help if a any… How can make my web app workable ?