我得到了一个模棱两可的 uri 匹配:Toyota-Corolla-vehicles/2
Iv'e 将问题隔离为这两条路线
[HttpGet("{make}-vehicles/{makeId:int}")]
[HttpGet("{make}-{query}-vehicles/{makeId:int}")]
对我来说,这看起来很明确。uri不应该与路由匹配两个破折号吗?
有关更多上下文: 我正在使用像这样的可读网址Toyota-vehicles-in-2005
.所以我不能使用正斜杠进行分隔。
[HttpGet("{make}-vehicles-in-{year}/{makeId:int}")]
文档指出:
复杂段(例如,[Route("/dog{token}cat")]),是 通过以非贪婪的方式从右到左匹配文字来处理。有关说明,请参阅源代码。有关详细信息,请参阅 这个问题。
https://github.com/aspnet/Routing/blob/9cea167cfac36cf034dbb780e3f783114ef94780/src/Microsoft.AspNetCore.Routing/Patterns/RoutePatternMatcher.cs#L296
https://github.com/aspnet/AspNetCore.Docs/issues/8197
我建议您使用以下路线:
[HttpGet("{make}/vehicles/{makeId:int}")]
[HttpGet("{make}/{query}/vehicles/{makeId:int}")]
在这种情况下,Toyota/vehicles/2
和Toyota/Corolla/vehicles/2
之间没有歧义。在你的例子中,由于事实,它有歧义,{query}
是string
的类型,所以Toyota-Corolla-vehicles
字符串同时匹配{make}-vehicles
和{make}-{query}-vehicles
,因为我们可以像这样解析它:
- 所有
{make}
参数都等于Toyota-Corolla
; {make}-{query}
等于Toyota-Corolla
,其中{make}
是Toyota
的,{query}
是相应的Corolla
。
所以,问题出在你的角色-
。如果您不想更改路线,可以只保留[HttpGet("{make}-vehicles/{makeId:int}")]
并通过string.Split
方法区分然后Toyota-Corolla
。