Asp.net mvc 为什么这两个API方法会产生冲突
我们正在开发一个MVC4API应用程序,遇到了一个我们无法解释的奇怪问题 API控制器有两种方法:Asp.net mvc 为什么这两个API方法会产生冲突,asp.net-mvc,asp.net-mvc-4,asp.net-web-api,Asp.net Mvc,Asp.net Mvc 4,Asp.net Web Api,我们正在开发一个MVC4API应用程序,遇到了一个我们无法解释的奇怪问题 API控制器有两种方法: [AllowAnonymous] [AcceptVerbs("Post", "Get")] public ArtifactContent Post(string userName, string password, string id) ... 及 尽管这两种方法显然具有不同的方法签名,但我们得到了以下错误消息: {“消息”:“出现错误。”,“异常消息”:“多个” 找到与
[AllowAnonymous]
[AcceptVerbs("Post", "Get")]
public ArtifactContent Post(string userName, string password, string id) ...
及
尽管这两种方法显然具有不同的方法签名,但我们得到了以下错误消息:
{“消息”:“出现错误。”,“异常消息”:“多个”
找到与请求匹配的操作:
[三十] .Models.ArtifactContentPost(System.String,System.String,
System.String)在类型上
[三十] .API.ArtifactContentController\r\nSystem.Net.Http.HttpResponseMessage
Get(System.String、System.String、System.String、ArtifactContentTypes)on-type
[三十] .API.ArtifactContentController,“异常类型”:“System.InvalidOperationException”,“StackTrace”:
在
System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext
controllerContext)\r\n位于
System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext
controllerContext)\r\n位于
System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext
controllerContext,CancellationToken CancellationToken)\r\n位于
System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage
请求,CancellationToken CancellationToken)\r\n位于
System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage
请求,CancellationToken CancellationToken)“}
我们可以通过做这两个更改中的一个来解决这个错误,但是我真的想知道为什么.NET在两个签名明显不同的情况下抛出这个错误:
Post
方法中删除Get
AcceptVerbs
属性Get
方法的签名以将枚举接受为整数这是
ASP.NET Web API
中的已知行为/问题/错误
简而言之,当尝试将传入HTTP请求与控制器内的相关操作相匹配时,操作选择器(IHttpActionSelector
)将不考虑enum
原因是默认情况下,只有基元类型(即int
、string
等)从RouteData
中选取以查找匹配操作<代码>枚举不是其中之一,因此它被忽略,因此,即使从编译器的角度来看,您的操作具有不同的签名,但在操作选择器的眼中,它们是相同的
您可以在此处跟踪潜在修复的进度-
目前,正如您自己所说,最好的解决方法是将enum
作为int
或string
传递(并转换为enum
)
[AllowAnonymous]
[AcceptVerbs("Get")]
public HttpResponseMessage Get(string userName, string password, string id, EnumType contentType) ...