0

I am very new to ASP.Net MVC and trying to learn ASP.Net MVC 5. I am trying to learn it by creating a sample project from scratch without scaffolding.I know that it is possible to specify the Default Controller and Default Action in the RouteConfig.cs, by calling the routes.MapRoute method. But I am wondering, if I can set the Default Controller & Action using Route attribute without using RouteConfig.cs.

Edit: I found that, it is possible to do this, by adding the following line of code in the RegisterRoutes method of RouteConfig.cs and removing the other lines.

routes.MapMvcAttributeRoutes();

After this, in the controller action where we want to set the default, just add the [Route("~/")] attribute above the action. This should do it.

But, in case of Forms Authentication we will have a login view. If we don't have traditional routing, then the redirection to Login view will fail because the attribute based routing for the default controller, which will be home, is not redirecting to Login. If we add the [Route("~/")] attribute to the Login action also, it throws an error saying "Multiple controller types were found that match the URL. This can happen if the attribute routes on multiple controllers match the requested URL."

robinCTS
  • 5,746
  • 14
  • 30
  • 37
Ven
  • 235
  • 4
  • 12

1 Answers1

0

The traditional routing and Attribute Routes can work together. Just don't remove other lines in RouteConfig.cs and put routes.MapMvcAttributeRoutes(); on top of other lines the Routing system at first will try to find a match with Attribute Routes for requested URL if it couldn't find match then will look for traditional routing system to find a match for requested URL

Ali Zeinali
  • 551
  • 4
  • 16