Routing in ASP.NET MVC

Routing in ASP.NET MVC

The ASP.NET Routing module is responsible for mapping the incoming browser requests (i.e. the incoming URL) to an MVC controller action method. This mapping is done by the routing rules defined for your application.

Routing enable us to define URL pattern that maps to the request handler. This request handler can be a file or class.

Route defines the URL pattern and handler information. All the configured routes of an application stored in RouteTable and will be used by Routing engine to determine appropriate handler class or file for an incoming request.

In simple word, we can say that ASP.NET MVC Routing is a pattern matching mechanism that handles the incoming request (i.e. incoming URL) and figures out what to do with that incoming request (i.e. incoming URL).

The following diagram illustrates the Routing process.

No alt text provided for this image

Where you can find Route Config class file?

Every MVC application must configure (register) at least one route in the RouteConfig class and by default MVC Framework provide one default route. But you can configure as many as routes you want. You can register a route in the RouteConfig class, which is in RouteConfig.cs file under the App_Start folder as shown below.

No alt text provided for this image

How to Configure a Route in ASP.NET MVC?

Routing is configured using the MapRoute() extension method of 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.

Defaults Parameter in routes.MapRoute() 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.

No alt text provided for this image

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 https://localhost:44380/{controller}/{action}/{id}.

Anything after the “https://localhost:44380/” would be considered as the controller name. 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 action name and id if it is required. If you will not provide any of the value, then default values of these parameters will be provided by the routing system that means the default controller and action method will handle the request.

No alt text provided for this image

How to Register Routes in ASP.NET MVC?

After configuring the routes in RouteConfig class, you need to register it in the Application_Start() event in the Global.asax file. So that it includes all your routes into the RouteTable.

No alt text provided for this image

Global.asax is located under Project folder which is shown below.

No alt text provided for this image

Thus we can that routing plays important role in MVC framework as it directs the request coming from outside world to the correct Controller which in turn will create the required responses for the user.

To view or add a comment, sign in

More articles by DHARSHAN KESAVAN

Insights from the community

Others also viewed

Explore topics