Web Api 获取:找到多个与请求匹配的操作
Web Api Get: Multiple actions were found that match the request
我在使用以下方法时遇到问题:
[Authorize]
[HttpGet]
[ResponseType(typeof(IEnumerable<Index.Model>))]
public async Task<IHttpActionResult> GetArchives()
[Authorize]
[HttpGet]
[ResponseType(typeof(Get.Query))]
public async Task<IHttpActionResult> GetArchive(Get.Query query)
它给我以下错误:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Multiple actions were found that match the request: GetArchives on type IAP.api.Controllers.ArchivesController GetArchive on type IAP.api.Controllers.ArchivesController
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
</Error>
我已经通读了针对提出相同问题的人的其他解决方案,但对我没有用(更具体地说 this one)。
我正在寻找不需要更改我的 Web Api 默认 REST 路由的解决方案,因为我还必须在客户端中更改它。
谢谢,
您可以在操作方法之上使用 [ActionName] 属性来指定一些不同的名称
[ActionName("archives")]
[ActionName("archive")]
现在您可以从客户端调用其操作名称。阅读此处了解更多信息:http://www.codeguru.com/csharp/.net/using-custom-action-names-in-asp.net-web-api.htm
实际上是相同路由的问题。您可以尝试使用 Route
属性来更改其中一种方法的路由,而不是整个路由方案。此外,使用这些属性,您可以尝试不更改路由名称 (GetArchives),但对第二种方法使用额外的路由约束。然后它会是这样的:
/archives/GetArchives
/archives/GetArchives/GetQueryConstraint
实现这个地方[Route("{query}")]
放在第二种方法上。如果这是您的自定义对象,那么您将需要创建自定义约束并指定它
[Route("{query:constraintType}")]
我在使用以下方法时遇到问题:
[Authorize]
[HttpGet]
[ResponseType(typeof(IEnumerable<Index.Model>))]
public async Task<IHttpActionResult> GetArchives()
[Authorize]
[HttpGet]
[ResponseType(typeof(Get.Query))]
public async Task<IHttpActionResult> GetArchive(Get.Query query)
它给我以下错误:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Multiple actions were found that match the request: GetArchives on type IAP.api.Controllers.ArchivesController GetArchive on type IAP.api.Controllers.ArchivesController
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
</StackTrace>
</Error>
我已经通读了针对提出相同问题的人的其他解决方案,但对我没有用(更具体地说 this one)。 我正在寻找不需要更改我的 Web Api 默认 REST 路由的解决方案,因为我还必须在客户端中更改它。
谢谢,
您可以在操作方法之上使用 [ActionName] 属性来指定一些不同的名称
[ActionName("archives")]
[ActionName("archive")]
现在您可以从客户端调用其操作名称。阅读此处了解更多信息:http://www.codeguru.com/csharp/.net/using-custom-action-names-in-asp.net-web-api.htm
实际上是相同路由的问题。您可以尝试使用 Route
属性来更改其中一种方法的路由,而不是整个路由方案。此外,使用这些属性,您可以尝试不更改路由名称 (GetArchives),但对第二种方法使用额外的路由约束。然后它会是这样的:
/archives/GetArchives
/archives/GetArchives/GetQueryConstraint
实现这个地方[Route("{query}")]
放在第二种方法上。如果这是您的自定义对象,那么您将需要创建自定义约束并指定它
[Route("{query:constraintType}")]