5

The RouteAttribute("abc", Order = 2) ordering only seems to be respected within a controller: when the following methods are defined in the same controller I get the expected behavior, i.e., Method1 takes priority, Method2 is never invoked.

[Route("abc", Order = 1)]
public ActionResult Method1() { ... }

[Route("abc", Order = 2)]
public ActionResult Method2() { ... }

If I define those methods in two separate controllers I get an ambiguous route exception: InvalidOperationException: Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

Is it possible to define the order across controllers using attribute routing?

Edit 1: It seems that even routes that would have an implicit ordering within one controller are ambiguous when spread over multiple controllers. The following would be okay within one controller (literal before wildcard), but throws an exception if placed into different controllers.

[Route("details/abc")]
[Route("details/{product}")]

This makes me that it is by design to keep controllers focused and force similar routes to be defined in the same controller.

Edit 2:

These are the routes I actually want to use. I want to put them into different controllers, because they do different things. They differ by the prefix.

[Route("reactive/monitor")]
[Route("{tier}/monitor")]
4

1 回答 1

0

对于第二条路线,您对路线参数没有任何限制。如果您要定义路线,则[Route("{tier:int}/monitor")]可能没有歧义。或者,您可以在路由中添加一个正则表达式,使它们独占,就像{tier:regex(^(?!reactive))?}可以让您解决这个问题。

于 2015-10-19T14:59:58.390 回答