We pay for user submitted tutorials and articles that we publish. Anyone can send in a contributionLearn More
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.
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.
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
These sections make Url Routing work so make sure you leave them alone. In Global.asax the default route table code looks like this:
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.
A trival example of a custom route would be as follows.
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.
Tags :ASP.NET MVCHTTPHandlersHTTPModulsMVCURL Routingm UrlWebServer
Copyright © 2012 Dev102.com
Breeze : Designed by Amit Raz and Nitzan Kupererd