我有一个有多个路由的控制器。
我正在尝试调用一个声明为的端点
GET: api/lookupent/2020-03-17T13:28:37.627691
Run Code Online (Sandbox Code Playgroud)
但这会导致此错误
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches:
Controllers.RecordController.Get (API)
Controllers.RecordController.GetRecordRegisteredAt (API)
Run Code Online (Sandbox Code Playgroud)
但我不确定我明白为什么这有意义,因为这段代码
// GET: api/{RecordName}/{id}
[HttpGet("{RecordName}/{id}", Name = "GetRecord")]
public ActionResult Get(string RecordName, long id)
// GET: api/{RecordName}/{timestamp}
[HttpGet("{RecordName}/{timestamp}", Name = "GetRecordRegisteredAt")]
public ActionResult GetRecordRegisteredAt(string RecordName, string timestamp)
Run Code Online (Sandbox Code Playgroud)
为什么输入与这些端点匹配?
您可以使用路由约束来解决此问题。
这是他们的例子:
[Route("users/{id:int}")]
public User GetUserById(int id) { ... }
[Route("users/{name}")]
public User GetUserByName(string name) { ... }
Run Code Online (Sandbox Code Playgroud)