Cookies on the SSM site: we use cookies and other technologies to ensure everyone who uses visits has the best possible experience. Cookies also help us keep your account safe.    Know more
SSM College of Engineering and Technology Articles : Routing in ASP .NET MVC 5

Routing in ASP .NET MVC 5..
0 comments    7757 views

Posted By » Aamir Muhammad
2nd July 2014

Routing is how ASP.NET MVC matches a URI to an action. MVC 5 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives more control over the URIs in a web application. The earlier style, called convention based routing, is still fully supported. In fact, both the techniques can be combined in the same project. :

The ASP.NET MVC is an open source web application framework that implements the model–view–controller (MVC) pattern.

Based on ASP.NET, ASP.NET MVC allows software developers to build a web application as a composition of three roles: ModelView and Controller. The MVC model defines web applications with 3 logic layers:

·         Model (business layer)

·         View (display layer)

·         Controller (input control)

model represents the state of a particular aspect of the application. A controller handles interactions and updates the model to reflect a change in state of the application, and then passes information to the view. A view accepts necessary information from the controller and renders a user interface to display that information.

In April 2009, the ASP.NET MVC source code was released under the Microsoft Public License (MS-PL).

ASP.NET MVC framework is a lightweight, highly testable presentation framework that is integrated with existing ASP.NET features. Some of these integrated features are master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly.

The ASP.NET MVC Framework couples the models, views, and controllers using interface-based contracts, thereby allowing each component to be tested independently.

The current version of MVC is 5.1.3 released on 22nd June, 2014. There have been some noticeable changes in the current release. Attribute Based Routing is one of these.

Routing in MVC

As MVC is all about creating loosely coupled applications, most of the features in MVC compliment creating loosely coupled architecture. Routing is one such MVC feature which decouples the URL schema of the application from the rest of the application.

In ASP.NET MVC, the request is handled by action methods. So there has to be some way to map the URL to the appropriate action method as there is no physical file to handle the requested URL. This mapping is provided by the routing system

In ASP.NET MVC since the request is handled by action methods there has to be some way to map the URL to the appropriate action method as there is no physical file to handle the requested URL. This mapping is provided by the routing system.

Attribute Based Routing

In previous versions, the routing was done by adding routes to the route collection when the application is initialized as the method is called from the Application_Start method in global.asax.

Now suppose we want to change the url-pattern that is handled by a specific action method in a controller we need to make the change in the common RegisterRoutes method. Doing this may cause some advantages:

Since RegisterRoutes is the common method which defines all the routes in the application ,our change could break the url handling of the application.

Since routing is setup when the application is launched we don’t have any easy way to change the routing schema of the application without effecting the existing routes in the application as we need to consider the order in which we have to add our route(as routes are evaluated from top to bottom).This requires proper testing for verifying if new bugs are introduced because of a newly added route.

Also keeping the route definition separate from the controller and action methods which is the handler of the request is not much intuitive.

Attribute based Routing provides a solution to this:

Using attribute based routing we can define the route in the same place where action method is defined. 

For instance:


        public ActionResult GetElectronicItems(int? id) {

            ViewBag.Id = id; return View();


To enable attribute routing we need to add following line in the RouteConfig file:

        public static void RegisterRoutes(RouteCollection routes)




So now we have attached the Route attribute to our action method our action method will be able to handle the requests which matches the URL pattern defined by the Route attribute.

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>