我已经安装了 Web-Api Route Debugger 来尝试弄清楚这里发生了什么,它报告了以下异常:-
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Multiple actions were found that match the request:
System.Net.Http.HttpResponseMessage GetTasksByParentAndList(Int32, Int32, Int32, Int32) on type AcademicStudio.WebHost.Controllers.TasksController
System.Net.Http.HttpResponseMessage GetTasksByListTreeAndDepartmentAndTerms(Int32, Int32, Int32, Int32, Int32[], Int32[]) on type AcademicStudio.WebHost.Controllers.TasksController
Source=System.Web.Http
StackTrace:
at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)
at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)
at AcademicServer.WebHost.Areas.RouteDebugger.InspectActionSelector.SelectAction(HttpControllerContext controllerContext) in d:\AcademicServer\AcademicServer.WebHost\Areas\RouteDebugger\InspectActionSelector.cs:line 42
at System.Web.Http.Tracing.Tracers.HttpActionSelectorTracer.<>c__DisplayClass2.<System.Web.Http.Controllers.IHttpActionSelector.SelectAction>b__0()
at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEnd(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Action execute, Action`1 endTrace, Action`1 errorTrace)
InnerException:
这两个动作都在控制器中使用 ActionName 属性声明如下:(并且具有不同的参数)。
[ActionName("ParentAndList")]
public HttpResponseMessage GetTasksByParentAndList(Int32 parentId, Int32 listId, Int32 fromId, Int32 toId)
{
var service = new TaskService();
var data = service.SelectTasksByParentAndList(parentId, listId, fromId, toId);
return Request.CreateResponse(HttpStatusCode.OK, data);
}
[ActionName("ListTreeAndDepartmentAndTerms")]
public HttpResponseMessage GetTasksByListTreeAndDepartmentAndTerms(Int32 parentId, Int32 listId, Int32 fromId, Int32 toId, [FromUri]Int32[] defs, [FromUri]Int32[] depts)
{
var service = new TaskService();
var data = service.SelectTasksByListTreeAndDepartmentAndTerms(parentId, listId, fromId, toId, defs, depts);
return Request.CreateResponse(HttpStatusCode.OK, data);
}
谁能解释发生了什么?控制器包含许多使用 ActionName 声明的其他操作,它们工作得很好。
更新。
发布问题十分钟后,我想通了: MapHttpRoutes were not proper configured in WebApiConfig.cs
为什么你可以在放弃并问一个问题后一分钱终于掉下来的整个上午和十分钟都盯着某样东西?