In older MVC versions, with the AttributeRouting library, I can have multiple routes and specify a precedence, so the most appropriate is picked when generating URLs:
[Route("", ActionPrecedence = 1)]
[Route("city/{citySlug}", ActionPrecedence = 2)]
In MVC 5 there is no ActionPrecedence
property on the attribute. How do I specify the route precedence in that case?
Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.
Attribute routing gives you more control over the URIs in your web application. The earlier style of routing, called convention-based routing, is still fully supported. In fact, you can combine both techniques in the same project.
As per our opinion, Attribute Routing provides us more flexibilities as compared to Convention Based Routing. In Attribute Routing, we can manage route as controller level, action level and also area level. Also, it can override the child route, if required.
Enabling attribute routing in your ASP.NET MVC5 application is simple, just add a call to routes. MapMvcAttributeRoutes() method with in RegisterRoutes() method of RouteConfig. cs file. You can also combine attribute routing with convention-based routing.
Are you using release version?
In Released version MVC 5.0, you can specify Name
and Order
for every Route
. The Order
is helpful in Url generation.
Route(template, NamedParams:[Name,Order])
[Route("city/{id}",Name="CityFirst", Order=1)]
[Route("mycity/{id}", Name = "MyCityFirst", Order = 2)]
Refer : Attribute Routing in ASP.NET MVC 5
UPDATE: My mistake! above answer was based on the RC1 assumed to be Released version.
In released version, there is no named attribute "Order".
Order of attribute is calculated based on the precedence of Route Template matching.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With