简体   繁体   中英

Multiple actions were found with WebAPI post - WHY isn't this working?

I've spent the whole day on this, I'm pretty much bald now.

Controllers:

[HttpPost]
public HttpResponseMessage AddSet(SetDto set)

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

Routes in order:

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

I call copy with POST /api/set/copt/ids , and add with POST /api/set . What am I doing wrong?

Full error:

"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",

I suppose you are getting the above error when making a request like 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)

In your routetemplate you only allow api/set/{id}. But none of your request follow that pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM