In a typical ASP.NET Web Form application, request is mapped to a physical file as follows:
1 2 3 4 5 |
//Displaying all employees http://locahost:XXXX/Employee.aspx //Displaying employee by Id http://locahost:XXXX/Employee.aspx?Id=10 |
1 2 3 4 5 |
//Displaying all employees http://locahost:XXXX/Employee/ //Displaying employee by Id http://locahost:XXXX/Employee/10/ |
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.
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.
1 2 3 4 5 6 7 |
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.
1 2 3 4 5 6 7 8 9 10 |
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.
- 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.
- 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:

Top 10 Interview Questions and Answers Series:
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions