ASP.NET MVC Routing

Kailash Chandra Behera | Friday, September 09, 2016

Introduction

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 features in ASP.NET MVC, It helps to map incoming browser request to the 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 a .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 a .aspx file. For example, a request for http://localhost/user/details.aspx?id=4 maps to a file that is named as.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)  
     {  
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
       routes.MapRoute(  
         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.

  RouteConfig.RegisterRoutes(RouteTable.Routes);  

Example

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.

  routes.MapRoute(  
         name: "product",  
         url: "{localhost}/{Pen}/{ id}",  
         defaults: new { controller = "product", action = "GetPen", id = UrlParameter.Optional } );  
       routes.MapRoute(  
         name: "pencil",  
         url: "{localhost}/{ Pencil}/{ id}",  
         defaults: new { controller = "product", action = "GetPen", id = UrlParameter.Optional });  
       routes.MapRoute(  
         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

Summary

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.

Thanks