Why do you need to specify the controller and action in the default route for the RoutesTable in ASP.NET MVC?

StackOverflow https://stackoverflow.com/questions/23092357

  •  04-07-2023
  •  | 
  •  

Question

Why do we have to specify the defaults for the default route?

This is a normal default route:

  routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  );

Why can't I just do this:

  routes.MapRoute(
        name: "Default",
        url: "Home/Index/{id}",
        defaults: new {id = UrlParameter.Optional } 
  );

I already specified the action and controller but when I use this way, I get an error. Does anyone know why you have to specify the action and controller in the default route?

Was it helpful?

Solution

Without a default set of parameters, how is routing supposed to know where to send this URL?

/

The defaults let you do that URL, so it knows to use the 'Home' controller's 'Index' method.

Or:

/Articles

In this case, the 'Index' action of the 'Articles' controller would be called. Without those defaults, again, routing has no way to know what to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top