DBA > Articles

ASP.NET MVC Routing Tutorial – Part I

To read more DBA articles, visit http://dba.fyicenter.com/article/

ASP.NET MVC routing serves two main purposes:

* Matching incoming requests and mapping them to a controller action.
* Constructing outgoing URLs which correspond to controller actions.

ASP.NET MVC Routing compared to URL Rewriting

Both Routing and URL ReWriting can create search engine friendly URLs. A key difference, however, is that URL Rewriting is an approach which focuses on a single page at a time. Most ASP.NET URL rewriting schemes rewrite a URL for one page to be handled by another. For example , /product/cars.aspx might be rewritten as: /products.aspx?id=65 .

Routing, by contrast. is a resource-centric view of URLs. A URL represents a resource which is not necessarily a web page. With Routing, this resource is a block of code that executes when an incoming request matches the route. A route determines how a request is dispatched based on characteristics of a URL.

Another difference is that Routing is bidirectional as it also helps in generating URLs by using the same rules used to match incoming URLs. However, ASP.NET Routing never actually rewrites a URL, the URL that a user requests in the browser is the same URL the application sees throughout the request lifecycle.

Defining Routes
An ASP.NET MVC app needs a minimum of one route to define how the app should handle requests, complex apps will likely have numerous routes. Route definitions begin with a URL that specifies a pattern the route will match. As well as the route URL, routes may also specify a default value and constraints for parts of the URL. This provides very tight control over how a route matches the incoming request URLs.

Route URLs
In the Application_Start method of the Global.asax.cs file in a ASP.NET MVC Web Application project, there is a call to the RegisterRoutes method. This method is the place where all the routes for the app are registered.
v For this tutorial we can remove all the routes in there and enter a single very simple route:

routes.MapRoute(“simple”, “{controller}/{action}/{id}“);

Here, the route URL consists of several URL segments (a segment is the text between slashes although not including the slashes). Each segment contains a placeholder delimited by curly braces. Placeholders are referred to as URL parameters. Controller is a required parameter which instantiates the controller class to handle the request. MVC will append the suffix Controller to the {controller} value and try to locate a type of that name which also inherits from the System.Web.Mvc.IController interface.

The action parameter is also required and is the method of the controller which is called to handle the current request.

All other parameters (except controller and action ) are passed as parameters to the action method.

For example, the url /articles/category/15 would result in the below controller:

public class ArticlesController : Controller
public ActionResult Category(int id)
//Perform Operations....

MVC would create this class and call the Category method to pass in the the id value of 15 as parameter to the Category method.

Routing can be very flexible in the URL patterns it parses, for example if the above Routing pattern was for the articles section of a CMS and the Forums section had a different pattern then the articles section could be differentiated by using the below route:

routes.MapRoute(“simple”, “articles/{controller}/{action}/{id}“);

This routing rule will only apply to URLs which commence with “articles/” and there a separate rule could be written for URLs commencing in “forums/”

That should get you started, in the next tutorial we will look at Defaults in Routing.

Full article...

Other Related Articles

... to read more DBA articles, visit http://dba.fyicenter.com/article/