Understanding Routing in ASP.NET MVC

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:

Routing Engine in ASP.NET MVC

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 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.

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.

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

How 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:

So, below code is applying a Routing Constraint that Id can contain only numeric values.

Routing is a key step in MVC request executing cycle.Purpose of this web development 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.

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.