Understanding ASP.NET MVC Routing

By | March 25, 2014
Routing plays an important role in an ASP.NET MVC Application execution flow. Basically, it maps request URL to a specific controller action using a Routing Table.In order to describe user’s actions, MVC framework uses friendly URLs against actions instead of mapping it to physical files as in case of an asp.net Web Form application.
In a typical ASP.NET Web Form application, request is mapped to a physical file as follows:
As opposite to above approach, MVC framework maps request URL to controller as follows:
In above example URL, “Employee” is a Controller. You can follow here to get complete understanding about Controllers and Action Methods in ASP.NET MVC.

The point to understand that how this mapping is performed in ASP.NET MVC framework?

Routing Engine in ASP.NET MVC

For mapping URLs to controller class actions, MVC framework uses a Routing Engine.ASP.NET MVC Routing

We can define Routing Rules for the engine, so that it can map incoming URLs to appropriate controller but default routing is already there for a newly created MVC application using Visual Studio.
If we create a new ASP.NET MVC application in Visual Studio and run it. We will have default page like the following:
Routing in ASP.NET MVC
Routing Engine uses routing rules that are defined in Global.asax file in order to parse the URL and find out the path of corresponding controller.We can find the following piece of code in Application_Start() method of Global.asax file.

Line 03 in above code snippet represents that routes are configured on application start by calling a static method “RegisterRoutes” in RouteConfig class.

We can find RouteConfig.cs file under App_Start  folder in ASP.NET MVC 5 application. If we follow this method in RouteConfig class, we will find one default configured route as follows. Line # 03 to #07 is configuring one default route.

Line 04 => is the name for the route.
Line 05 => represent that URL will be our controller, then action followed by id (if any).
Line 06 => default controller will be Home, default action will be Index and Id is optional.

In above case, Routing Engine maps URL “http://localhost:11517/” to corresponding controller as:

  • It looks for Controller Segment in URL which is empty, so it takes the default controller i.e. “Home”.
  • Then it looks for Controller Action which is also not there, so it again takes the default controller action i.e. “Index”
  • Id is not provided and its optional, so Routing Engine will ignore it.
  • Learn ASP NET MVC 5 step by step [Maruti Makwana, Corporate Trainer] 28 Lectures, 2.5 Hours Video, Intermediate Level
    Very easy to learn video series on Asp.Net MVC 5 Specially for those who are familiar with Asp.Net Web forms.
  • AngularJS for ASP.NET MVC Developers [Brett Romero] 10 Lectures, 1 hour video, Intermediate Level
    The Fastest Way For .NET Developers To Add AngularJS To Their Resume
  • ASP.NET with Entity Framework from Scratch
    [Manzoor Ahmad, MCPD | MCT] 77 Lectures, 10 hour video, All Level
    Latest approach of web application development
  • Comprehensive ASP.NET MVC [3D BUZZ] 34 lectures, 14 Hours Video, All Levels
    From zero knowledge of ASP.NET to deploying a complete project to production.
Finally Routing Engine evaluates something as “http://localhost:11517/Home/Index“, where Home is our controller and Index is its Action. So, request will be forwarded to Home Controller’s Index Action. Because of the default routing rule, request is forwarded to same controller action in all following cases:
  • http://localhost:11517/
  • http://localhost:11517/Home
  • http://localhost:11517/Home/Index

But if we wanted to access some other controller say “Employee”, we will provide it explicitly as:

  • http://localhost:11517/Employee
  • http://localhost:11517/Employee/Index

Exam 70-486How to apply Routing Constraints in ASP.NET MVC?

In certain case, we need to apply some validation on a defined route, for example, we might want to restrict Id part of a URL to numeric only. Such validations are called Routing Constraints in ASP.NET MVC. Consider we have registered following route:Register Route in ASP.NET MVC

So, below code is applying a Routing Constraint that Id can contain only numeric values.Routing Constraint in ASP.NET MVC
Routing is a key step in MVC request executing cycle.Purpose of this ASP.NET MVC tutorial is to provide a detailed understanding of Routing in ASP.NET MVC application. If you want to read about a new type of Routing i.e. Attribute Routing, Click Here.

Top 10 Interview Questions and Answers Series: