为什么这两种API方法会产生冲突

Sha*_*hen 4 asp.net-mvc asp.net-mvc-4 asp.net-web-api

我们正在开发MVC4 API应用程序,并且遇到了一个我们无法解释的奇怪问题。

API控制器有两种方法:

    [AllowAnonymous]
    [AcceptVerbs("Post", "Get")]
    public ArtifactContent Post(string userName, string password, string id) ...
Run Code Online (Sandbox Code Playgroud)

    [AllowAnonymous]
    [AcceptVerbs("Get")]
    public HttpResponseMessage Get(string userName, string password, string id, EnumType contentType) ...
Run Code Online (Sandbox Code Playgroud)

尽管这两种方法显然具有不同的方法签名,但我们收到以下错误消息:

{“ Message”:“发生错误。”,“ ExceptionMessage”:“找到多个与请求匹配的操作:[XXX] .Models.ArtifactContent Post(System.String,System.String,System.String)类型[XXX] .API.ArtifactContentController \ r \ nSystem.Net.Http.HttpResponseMessage Get(System.String,System.String,System.String,ArtifactContentTypes)类型为[XXX] .API.ArtifactContentController“,” ExceptionType“:” System.InvalidOperationException“,” StackTrace“:”在System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\ r \ n在系统上.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\ r \ n在System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext,CancellationToken cancellingToken)\ r \ n在System.Web.Http.Dispatcher.HttpControllerDispatcher处。在System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage请求,CancellationToken取消令牌)上的SendAsyncInternal(HttpRequestMessage请求,CancellationToken取消令牌)\ r \ n

我们可以通过以下两个更改之一来解决错误,但是我真的想知道为什么当两个签名明显不同时.NET会引发错误:

  1. Get AcceptVerbsPost方法中删除属性
  2. 更改Get方法的签名以将Enum接受为整数

Fil*_*p W 5

这是中的已知行为/问题/错误ASP.NET Web API

简而言之,在尝试将传入的HTTP请求与控制器内部的相关动作进行匹配时IHttpActionSelector,不会考虑动作选择器()Enums

原因是在默认情况下只有基本类型(即intstring等),从采摘RouteData到找到匹配的作用。Enum并不是其中之一,因此将其忽略,因此,即使从编译器的角度来看,您的动作具有不同的签名,但在动作选择器的眼中,它们是相同的。

您可以在此处跟踪潜在修复程序的进度-http: //aspnetwebstack.codeplex.com/workitem/312

就像您自己说的那样,目前最好的解决方法是将enumas intstring(并强制转换为enum)。