控制器操作首先抛出 AmbiguousMatchException,然后在移除一个 [sic] 时抛出 NotImplementedException
Controller action throws AmbiguousMatchException at first, then NotImplementedException when one [sic] is removed
我正在将一个较旧的 API 项目从 .NET Framework 4.7.2 移植到 .NET 5,我想保留与现有项目相同的 API 签名以保留 API兼容性。这证明很麻烦。 (对于这个问题,忽略上下文;我只是想说明为什么我不想更改 URL 路由 。)
其中一个 .NET 5 项目的控制器具有带有此签名的操作:
[HttpGet]
[Route("{processedId}")]
// I had also tried [Route("{processedId:int}")] -- EDIT: but missed that there was a different exception when I did.
public async Task<TypedActionResult<Question>> GetQuestionByProcessedId(int processedId)
当我尝试使用此 URL 访问时:
https://localhost:12345/api/questions/2
...我收到以下信息:
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:
The request matched multiple endpoints. Matches:
My.Namespace.Api.Controllers.MyController.GetQuestionsForFolder (QuestionBuilder.Api)
My.Namespace.Api.Controllers.MyController.GetQuestionByProcessedId (QuestionBuilder.Api)
该错误中提到的其他控制器操作具有此签名:
[HttpGet]
[Route("{folderId=folderId:Guid}")]
public async Task<TypedActionResult<IEnumerable<Question>>> GetQuestionsForFolder(Guid folderId)
如果我注释掉有问题的、“模棱两可的”匹配项并拨打新电话,我会收到...
System.NotImplementedException: The method or operation is not implemented.
如果我通过注释掉预期的终点 GetQuestionByProcessedId
并仅保留 GetQuestionsForFolder
.
来翻转实验,则会出现相同的错误
请注意,我 可以 使用以下 URL 成功访问 GetQuestionsForFolder
端点(当它未被注释掉时,natch):
https://localhost:12345/api/questions/?folderId=87654321-1234-1234-1234-123456789012
问题
这些错误似乎有冲突。在一种情况下,我有 太多 端点准备好为 GET 请求提供服务。当我删除两者之一时,我 有 none.
如何让 URL 找到正确的控制器动作,为什么它看到两个然后又看到 none?
说得过于清楚,我知道我很可能 merge the endpoints 解决这个问题。我的问题是为什么我有两个端点获取请求,然后,当一个端点被删除时,none.
The request matched multiple endpoints
要解决上述错误,您可以为每个端点设置 route constraint。
例如:
[HttpGet]
[Route("{processedId:int}")] // the parameter is int type, allow the request with int parameter
public async Task<IActionResult> GetQuestionByProcessedId(int processedId)
{
return Ok("value: " + processedId.ToString());
}
[HttpGet]
[Route("{folderId=folderId:Guid}")] //the parameter is guid type, allow the request with guid parameter
public async Task<IActionResult> GetQuestionsForFolder(Guid folderId)
{
return Ok("value: " + folderId.ToString());
}
结果如下:
此外,您还可以在route/requesturl中添加动作名称,代码如下:
[HttpGet]
[Route("getQuestionByProcessedId/{processedId}")] // the parameter is int type
public async Task<IActionResult> GetQuestionByProcessedId(int processedId)
{
return Ok("value: " + processedId.ToString());
}
[HttpGet]
[Route("getQuestionsForFolder/{folderId=folderId:Guid}")] //the paramer is guid type
public async Task<IActionResult> GetQuestionsForFolder(Guid folderId)
{
return Ok("value: " + folderId.ToString());
}
然后请求URL像这样:
https://localhost:44310/api/todo/getQuestionsForFolder/87654321-1234-1234-1234-123456789012
https://localhost:44310/api/todo/getQuestionByProcessedId/2
输出如下:
我正在将一个较旧的 API 项目从 .NET Framework 4.7.2 移植到 .NET 5,我想保留与现有项目相同的 API 签名以保留 API兼容性。这证明很麻烦。 (对于这个问题,忽略上下文;我只是想说明为什么我不想更改 URL 路由
其中一个 .NET 5 项目的控制器具有带有此签名的操作:
[HttpGet]
[Route("{processedId}")]
// I had also tried [Route("{processedId:int}")] -- EDIT: but missed that there was a different exception when I did.
public async Task<TypedActionResult<Question>> GetQuestionByProcessedId(int processedId)
当我尝试使用此 URL 访问时:
https://localhost:12345/api/questions/2
...我收到以下信息:
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:
The request matched multiple endpoints. Matches:
My.Namespace.Api.Controllers.MyController.GetQuestionsForFolder (QuestionBuilder.Api)
My.Namespace.Api.Controllers.MyController.GetQuestionByProcessedId (QuestionBuilder.Api)
该错误中提到的其他控制器操作具有此签名:
[HttpGet]
[Route("{folderId=folderId:Guid}")]
public async Task<TypedActionResult<IEnumerable<Question>>> GetQuestionsForFolder(Guid folderId)
如果我注释掉有问题的、“模棱两可的”匹配项并拨打新电话,我会收到...
System.NotImplementedException: The method or operation is not implemented.
如果我通过注释掉预期的终点 GetQuestionByProcessedId
并仅保留 GetQuestionsForFolder
.
请注意,我 可以 使用以下 URL 成功访问 GetQuestionsForFolder
端点(当它未被注释掉时,natch):
https://localhost:12345/api/questions/?folderId=87654321-1234-1234-1234-123456789012
问题
这些错误似乎有冲突。在一种情况下,我有 太多 端点准备好为 GET 请求提供服务。当我删除两者之一时,我 有 none.
如何让 URL 找到正确的控制器动作,为什么它看到两个然后又看到 none?
说得过于清楚,我知道我很可能 merge the endpoints 解决这个问题。我的问题是为什么我有两个端点获取请求,然后,当一个端点被删除时,none.
The request matched multiple endpoints
要解决上述错误,您可以为每个端点设置 route constraint。
例如:
[HttpGet]
[Route("{processedId:int}")] // the parameter is int type, allow the request with int parameter
public async Task<IActionResult> GetQuestionByProcessedId(int processedId)
{
return Ok("value: " + processedId.ToString());
}
[HttpGet]
[Route("{folderId=folderId:Guid}")] //the parameter is guid type, allow the request with guid parameter
public async Task<IActionResult> GetQuestionsForFolder(Guid folderId)
{
return Ok("value: " + folderId.ToString());
}
结果如下:
此外,您还可以在route/requesturl中添加动作名称,代码如下:
[HttpGet]
[Route("getQuestionByProcessedId/{processedId}")] // the parameter is int type
public async Task<IActionResult> GetQuestionByProcessedId(int processedId)
{
return Ok("value: " + processedId.ToString());
}
[HttpGet]
[Route("getQuestionsForFolder/{folderId=folderId:Guid}")] //the paramer is guid type
public async Task<IActionResult> GetQuestionsForFolder(Guid folderId)
{
return Ok("value: " + folderId.ToString());
}
然后请求URL像这样:
https://localhost:44310/api/todo/getQuestionsForFolder/87654321-1234-1234-1234-123456789012
https://localhost:44310/api/todo/getQuestionByProcessedId/2
输出如下: