HarshSharma
HarshSharma

Reputation: 660

Route Config Vs Attribute based routing mvc whose priority is more

I have been asked by someone that, suppose i have defined routing for a URL in route.config and i have defined the same routing in attribute based routing. Then whose priority will be more in each case. And what is the use of attribute based routing if we can achieve the same in route.config.

Upvotes: 0

Views: 1097

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

Then whose priority will be more in each case.

That would depend on whether you call the routes.MapMvcAttributeRoutes() extension method before or after the conventional routes. For example:

public static void RegisterRoutes(RouteCollection routes)
{
    ...
    routes.MapMvcAttributeRoutes(); //Attribute routing

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

In this case the attribute based routes will be added first to the routing table and will take precedence.

And what is the use of attribute based routing if we can achieve the same in route.config.

Attribute routes give you quite a bit more flexibility and places the routes next to the actions that will actually use them. But it's really a matter of preference. Microsoft has added attribute based routes in order to have an alternative way for defining the routes in the application compared to the conventional approach.

Upvotes: 5

Related Questions