Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Route precedence with attribute routing

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?

like image 287
CMircea Avatar asked Oct 23 '13 21:10

CMircea


People also ask

What is routing and attribute routing?

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.

What is the advantage of attribute routing?

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.

What is difference between attribute and conventional routing?

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.

What is the method for enabling attribute routing?

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.


1 Answers

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.

like image 164
jd4u Avatar answered Sep 17 '22 14:09

jd4u