In this article, I am going to discuss Routing in the ASP.NET MVC Application with Examples. Routing is one of the most important features of the ASP.NET MVC Framework. As part of this article, we are going to discuss the following pointers which are related to ASP.NET MVC Routing.
- What is Routing in MVC?
- Understanding Route and Route Table in ASP.NET MVC.
- What are the different types of Routing supported by ASP.NET MVC?
- How does the Routing work in ASP.NET Web Form?
- What is a Route in MVC Application?
- How Does the Routing work in ASP.NET MVC Application?
- How to Configure a Route in ASP.NET MVC?
- Understanding the URL Pattern in ASP.NET MVC Routing
- How to Register Routes in ASP.NET MVC?
- Examples to understand Routing.
What is Routing in ASP.NET MVC?
The ASP.NET MVC Routing module is responsible for mapping the incoming browser requests (i.e. the incoming URL or incoming HTTP Requests) to a particular controller action method. This mapping is done by the routing rules defined for your application. For example, if we issue a request to the “/Home/Index†URL, then it is the Index action method of the Home Controller class that is going to handle the request as shown in the below image.
Routing is not new or specific to the ASP.NET MVC framework. It can also be used with our traditional ASP.NET WebForms application.
What are the different types of Routing supported by ASP.NET MVC?
In the ASP.NET MVC application, we can define routes in two ways. They are as follows:
- Convention Based Routing
- Attribute-Based Routing.
In this Video, I am going to discuss the Convention Based Routing in ASP.NET MVC Application. In our upcoming Video, we will discuss the . Before understanding how routing works in ASP.NET MVC, let us first understand how the routing works in our traditional Web Forms Application.
How does the Routing work in ASP.NET Web Form?
As we know in ASP.NET Web Forms application, each and every URL should be matched with a specific .aspx file. For example, a URL http://dotnettutorials/employeeinfo.aspx must match with a physical file i.e. employeeinfo.aspx that contains necessary code and HTML for rendering the response to the browser. So in the case of ASP.NET Web Forms, the URL pointing to the file must have its physical existence.
Then ASP.NET Framework introduced the concept of Routing to eliminate the need of mapping each and every URL to a physical file. The Routing Concept enables us to define the URL pattern that maps to the request handler. That request handler can be a class (class methods) or file.
In the case of the ASP.NET Webform application, the request handler is a file (i.e. aspx file) and in the case of ASP.NET MVC Framework, the request handler is the Controller Methods i.e. action methods. For example, http://dotnettutorials/employees can be mapped to http://dotnettutorials/employeeinfo.aspx in ASP.NET Webforms application and the same URL can be mapped to Employee Controller and Index action method in ASP.NET MVC application.
What is a Route in ASP.NET MVC Application?
The Route defines the URL pattern and the handler information. The handler can be a physical file, such as an ASPX file in the case of the WebForms application. A handler can also be a class that processes the request, such as a controller in the case of the ASP.NET MVC application.
In the ASP.NET MVC application, all the Routes (URL pattern and Handler Information) are stored in the RouteTable and then the Routing engine uses this RouteTable to determine the appropriate handler class for an incoming HTTP request. Now, I hope you got some basic idea about Route. Let us proceed further and understand how does the routing works in the ASP.NET MVC Application in detail.
How Does the Routing work in ASP.NET MVC Application?
Please have a look at the following diagram which illustrates the Routing Process in the ASP.NET MVC Application.
In simple words, we can say that Routing in ASP.NET MVC is a pattern matching mechanism that handles the incoming HTTP request (i.e. incoming URL) and figures out what to do with that incoming HTTP request.
When the client makes a request i.e. makes an HTTP Request, then that request is first received by the Routing Engine. Once the Routing engine receives an HTTP Request, then it figures out the URL Pattern of the incoming request and checks if that URL pattern is present in the Route table. If it found a matching URL pattern for the incoming request in the Route Table, then it fetches the corresponding handler information and forwards the request to the appropriate controller and action method. If there is no match found in the routing table for the incoming HTTP request URL Pattern, then it simply returns a 404 HTTP status code. The routing functionality is implemented in the System.Web.Routing.
How to Configure a Route in ASP.NET MVC?
Every ASP.NET MVC application must configure (register) at least one route in the RouteConfig class and by default, the ASP.NET MVC Framework provides one default route. But you can configure as many routes as you want. You can register a route within the RegisterRoutes method of RouteConfig class, which you can find with the RouteConfig.cs class file under the App_Start folder. You will find the following code in the RouteConfig class.
Default Route in ASP.NET MVC?
The following image shows the default route of the ASP.NET MVC application which is created by default when we create a new ASP.NET MVC 5 Application.
As you can see in the above image, the Routing is configured using the MapRoute() extension method of the RouteCollection class, where the Route name is “Default†and the URL pattern is “{controller}/{action}/{id}“. The Defaults value for the controller is Home, and the default action method is Index and the id parameter is optional. This Route information i.e. the Route Name, URL Pattern, and handler information i.e. the controller name, action method name are stored in the Route table at the application start-up i.e. when the application runs for the first time.
Here Defaults specifies which controller, action method, or value of id parameter should be used if they do not exist in the incoming request URL. In the same way, you can configure other routes using the MapRoute method of RouteCollection. That we will discuss in our
Note: The route name should be unique across the entire application. Route name cant be duplicated.
Understanding the URL Pattern in ASP.NET MVC Routing:
The URL pattern is considered only after the domain name in the URL. For example, Suppose your web application is running on www.dotnettutorials.net then the URL pattern “{controller}/{action}/{id}†for your application would be look like www.dotnettutorials.net/{controller}/{action}/{id}.
Anything after the “www.dotnettutorials.net/†would be considered as the controller name. In the same way, anything after the controller name would be considered as the action name and the value of the id parameter.
Hence you need to provide the controller name followed by the action name and id if it is required. If you will not provide any of the values then default values of these parameters will be provided by the routing engine that means the default controller and action method will handle the request.
How to Register Routes in ASP.NET MVC?
Once you create the Route, next we need to register the Route at the Application Level. To register something at the application level, ASP.NET MVC Framework provides the Global.asax file. This file contains one class called MvcApplication which is inherited from the System.Web.HttpApplication class. This class provides many methods such as Application_BeginRequest, Application_Error, Application_Start, Session_Start, Session_End, etc. So, the question is in which method, we need to register the Routes? We want the Route Table to load all the Routes at the Application Startup i.e. when we run the application for the first time. So, we need to register the routes within the Application_Start method by calling RouteConfig.RegisterRoutes(RouteTable.Routes) method. If you open the Global.asax file, you will find the following code in it.
Global.asax
Once you call RouteConfig.RegisterRoutes(RouteTable.Routes) method, then notice here, we are passing the RouteCollection (i.e. RouteTable.Routes) as an input parameter and if you further notice, within the RegisterRoutes method, we add the routes to this RouteCollection property.
Understanding ASP.NET MVC Routing with an Example:
To understand ASP.NET MVC Routing lets create a controller called HomeController as shown below
Example to understand Routing:
- http://localhost:53605/ => controller = Home, action = Index, id = none, since default value of controller and action are Home and Index respectively.
- http://localhost:53605/Home => controller = Home, action = Index, id = none, since default value of action is Index
- http://localhost:53605/Home/Index => controller = Home, action = Index, id=none
- http://localhost:53605/Home/Index/5 => controller = Home, action = Index, id = 5
The default route that ASP.NET MVC Framework creates for you when you create a new ASP.NET MVC 5 application assumes that you will follow this convention approach. But if you want to follow your own convention then you need to modify the routes or you need to create your own routes that we will discuss in our next Video.
How is the routing table created in ASP.NET MVC?
When an MVC application first starts, the Application_Start() method in global.asax is called. This method calls the RegisterRoutes() method. The RegisterRoutes() method creates the routing table for the ASP.NET MVC application.