4

我花了一整天的时间在这上面,我现在秃顶了。

控制器:

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

copyPOST /api/set/copt/ids和打电话addPOST /api/set我究竟做错了什么?

完整错误:

"exceptionMessage": "找到与请求匹配的多个操作:\r\nSystem.Net.Http.HttpResponseMessage AddSet(App.Repository.Models.Dtos.SetDto) 类型为 App.Service.Controllers.SetController\r\nSystem。 Net.Http.HttpResponseMessage

类型 App.Service.Controllers.SetController 上的 CopySet(Int32[])",

4

2 回答 2

5

我想您在发出类似的请求时遇到了上述错误POST /api/set/copt/ids

Web API is strict about matching route variable names to the action parameter names.

Try doing the following and see(note: the Name parameter in FromUri would map the route variable name to your parameter here...this is called aliasing):

[HttpPost]
[ActionName("copy")]
public HttpResponseMessage CopySet([FromUri(Name="id")]int[] ids)
于 2013-07-22T23:19:38.063 回答
-2

在您的路由模板中,您只允许 api/set/{id}。但是您的任何请求都没有遵循这种模式。

于 2013-07-22T23:06:30.137 回答