Updated on 29 Sep 2025 by Admin

Routing in Asp.Net MVC

If you have gone through my previous article, you should have a little idea about the ASP.NET MVC request-response mechanism.

In ASP.NET MVC, we do not directly request a physical page like we used to in ASP.NET WebForms; instead, the URL consists of a Controller and an Action name, and the controller's Action Method handles the request and processes it. Finally, we get the desired response, whether it be in HTML, JSON, or XML .

Now, wait and think, how the request is actually being served, how the URL is being mapped with the appropriate Controller and Action, who is doing that?

Don't you think there is a third person who is extracting the Controller name and Action name from the URL and then mapping it to the appropriate Controller and Action Method for processing?

That third person is the Routing Module, also called HttpRoutingModule.

Now, you can say a route is a URL pattern, and the mechanism that monitors the incoming request and determines which controller and which action will handle the request for further processing is Routing.

As any Controller and its Action Method finally process the request, they are called the Handler.

Actually, in the MVC life cycle, routing is the first state.

Routing in MVC
Routing in MVC

Now, the question arises: how does the routing module actually work?

The routing engine uses the route table for matching the incoming URL. A route table can be considered a collection of different routes.

We can register as many routes (URL patterns) as we want, but at least one is necessary.

Now, you will be curious to know how to register a route and where the route table actually resides.

How to Define Routing in MVC:

In the Global.asax file, in the application_start event, the RegisterRoute method of the RouteConfig class is called, which is a static method.

So as the application starts, all the routes defined in the RegisterRoute method get registered in the RouteTable.

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

Now, go to AppStart-->RouteConfig.cs-->RegisterRoute().

In short, the RouteConfig is the class that is responsible for registering routes. In the RouteConfig class, the RegisterRoute method is defined, which has a parameter of type RouteCollection.

This RouteCollection is nothing but a get-only static property of the RouteTable class where you want your URL pattern to be registered.

The RouteCollection has an extension method, mapRoute which expects three parameters:-

  1. Name: The name of your URL pattern must be unique.
  2. URL: The URL pattern.
  3. Defaults: Default values for your Controller and Action.

By specifying the above parameters, you can register your routes. Let's understand it with some examples:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

In an MVC application, a route table must have at least one route definition. In the above example, the route template named "Default" is added to the route table. The URL pattern contains the controller and action placeholder. The default values tell the routing engine to invoke the default value if any of the placeholders are empty, meaning when the corresponding value is not supplied.

The IgnoreRoute method defined above takes care of the files that are not for routing and should be ignored, like files with extension .axd will be auto-ignored, and no one can fetch such a file from the URL.

URL matching depends on the following conditions:

  • The default value that is added in the route table.
  • The order in which the route is added.
  • The constraint that is provided for the route.
  • Routing pattern that defines the route.

As the order of routes matters, it is recommended to put the more generic routes at the bottom. For example, the default route should always come after the custom routes.

Let's create an Action in controller and define custom route for that.

public ContentResult GetArticle(string atricleName,string TopicName,string subtopicName)
{
return Content("Article :-"+atricleName+"Topic:-"+TopicName+"subtopic:-"+subtopicName);
}

Here, the GetArticle method is expecting 3 parameters. If we want these parameters to be passed in the URL as routed data, we will have to define a route for the same as follows:

routes.MapRoute(
name: "Article",
url: "article/{atricleName}/{TopicName}/{subtopicName}",
defaults: new {Controller="Article",Action="GetArticle" atricleName = "", TopicName = "", subtopicName = "" }
);

In the above route definition, I have not used any placeholder for Controller and Action, so in the URL, we do not need to specify any controller or action name; the default value will take care of that. Any URL that starts with the article will be mapped to the Article controller and the GetArticle action. We just need to provide the rest of the three parameters, or they will be null by default.


You need to login to post a comment.

Sharpen Your Skills with These Next Guides