Understanding Routing in ASP.NET MVC

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:

//Displaying all employees
http://locahost:XXXX/Employee.aspx

//Displaying employee by Id

http://locahost:XXXX/Employee.aspx?Id=10

As opposite to above approach, MVC framework maps request URL to controller as follows:

//Displaying all employees
http://locahost:XXXX/Employee/

//Displaying employee by Id

http://locahost:XXXX/Employee/10/

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.

protected void Application_Start()
{
01.    AreaRegistration.RegisterAllAreas();
02.    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
03.   RouteConfig.RegisterRoutes(RouteTable.Routes);
04.    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

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.

public static void RegisterRoutes(RouteCollection routes)
{
01.   routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
02.
03.  routes.MapRoute(

04.      name: “Default”,
05.      url: “{controller}/{action}/{id}”,
06.      defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional }
07.      );
}

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

Top 10 Interview Questions and Answers Series: