Rob*_*ous 4 c# asp.net-mvc routing asp.net-web-api
我花了一整天的时间,我现在很秃头.
控制器:
[HttpPost]
public HttpResponseMessage AddSet(SetDto set)
[HttpPost]
[ActionName("copy")]
public HttpResponseMessage CopySet([FromUri]int[] ids)
Run Code Online (Sandbox Code Playgroud)
路线顺序:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
routes.MapHttpRoute(
name: "Set",
routeTemplate: "api/set/{id}",
defaults: new { controller = "set", id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
我打电话copy用POST /api/set/copt/ids,并add用POST /api/set.我究竟做错了什么?
完整错误:
"exceptionMessage":"在App.Service.Controllers.SetController\r \nSystem类型上找到了与请求匹配的多个操作:\ r \nSystem.Net.Http.HttpResponseMessage AddSet(App.Repository.Models.Dtos.SetDto). Net.Http.HttpResponseMessage
App.Service.Controllers.SetController类型上的CopySet(Int32 [])",
我想你在提出这样的请求时遇到上述错误POST /api/set/copt/ids?
Web API严格将路由变量名称与操作参数名称匹配.
尝试执行以下操作并查看(注意:FromUri中的Name参数会将路径变量名称映射到此处的参数...这称为别名):
[HttpPost]
[ActionName("copy")]
public HttpResponseMessage CopySet([FromUri(Name="id")]int[] ids)
Run Code Online (Sandbox Code Playgroud)