MVC Routing


This article introduce about Routing in ASP.NET MVC, how it works and demonstrates how to configure custom routing for your project.

Getting Started

Routing is one of the most important feature in ASP.NET MVC,It helps to mapping incoming browser request to particular action of controller and Routing can be used to hide data which is not intended to be shown to the final user as well.

In Routing URL Patterns are defined that map to request-handler, the handler can be a physical file, such as an .aspx file in a Web Forms application. A handler can also be a class that processes the request, such as a controller in an MVC application

if your MVC project does not use routing, an incoming request for a URL typically maps to a physical file that handles the request, such as an .aspx file. For example, a request for http://localhost/user/details.aspx?id=4 maps to a file that is named details.aspx that contains code and markup for rendering a response to the browser. The Web page uses the query string value of id=4 to determine what type of content to display.

When you create MVC Project using Visual Studio 2015, you will find RoutingConfig in the App_Start folder, where routing is configuring for the project.

  public class RouteConfig  
     public static void RegisterRoutes(RouteCollection routes)  
         name: "Default",  
         url: "{controller}/{action}/{id}",  
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  

By using only above code routing will not work, two important places are there that helps to enable routing in MVC that is web.config file and Global.aspx file. in config file there are four sections that enable routing that are system.web.httpModules, system.web.httpHandlers, system.webserver.modules and system.webserver.handlers. In Global.asp file the Application_start event registers routing for MVC Project.



Let's say there is a controller named 'Product' and in that controller three actions are there with name GetPen,GetPencil and GetInk. Each action is responsible for listing respective product as name suggest. we will do rout in such a way that when user enter pen in the url the request will come to GetPen action and same routing for remaing actions. Below code example describes with code how to achieve this.

         name: "product",  
         url: "{localhost}/{Pen}/{ id}",  
         defaults: new { controller = "product", action = "GetPen", id = UrlParameter.Optional } );  
         name: "pencil",  
         url: "{localhost}/{ Pencil}/{ id}",  
         defaults: new { controller = "product", action = "GetPen", id = UrlParameter.Optional });  
         name: "Ink",  
         url: "{localhost}/{ Ink}/{ id}",  
         defaults: new { controller = "product", action = "GetInk", id = UrlParameter.Optional });  

The URLs for the above rout are like below
1. localhost:port/Pen
2. localhost:port/Pencli
3. localhost:port/Ink


In the above of this article we have discussed about ASP.NET MVC Rout and with code examples, Hope this article may helpful to you.