c# – 在WebAPI帖子中找到了多个操作 – 为什么这不起作用?

我花了一整天的时间,我现在很秃头.

控制器:

[HttpPost]
public HttpResponseMessage AddSet(SetDto set)

[HttpPost]
[ActionName("copy")]
public HttpResponseMessage CopySet([FromUri]int[] ids)

路线顺序:

        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 }
            );

我用POST / api / set / copt / ids调用copy,并用POST / api / set添加.我究竟做错了什么?

完整错误:

“exceptionMessage”: “Multiple actions were found that match the
request: \r\nSystem.Net.Http.HttpResponseMessage
AddSet(App.Repository.Models.Dtos.SetDto) on type
App.Service.Controllers.SetController\r\nSystem.Net.Http.HttpResponseMessage

CopySet(Int32[]) on type App.Service.Controllers.SetController”,

解决方法:

我想你在发出像POST / api / set / copt / ids这样的请求时会遇到上述错误?

Web API严格将路由变量名称与操作参数名称匹配.

尝试执行以下操作并查看(注意:FromUri中的Name参数会将路径变量名称映射到此处的参数…这称为别名):

[HttpPost]
[ActionName("copy")]
public HttpResponseMessage CopySet([FromUri(Name="id")]int[] ids)
上一篇:php – Silex路由.htaccess webroot


下一篇:ELS elasticsearch 从入门到放弃-安装运行