Numerous actions have been detected using the WebAPI post - WHY does this not work?

I spent the whole day on it, now I'm pretty bald.

Controllers

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

Routes are 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": "Several actions were found that match request: \ r \ nSystem.Net.Http.HttpResponseMessage AddSet (App.Repository.Models.Dtos.SetDto) by type App.Service.Controllers.SetController \ g \ nSystem. Net.Http.HttpResponseMessage

CopySet (Int32 []) for type App.Service.Controllers.SetController ",

+4
source share
2 answers

I suppose you get the above error when creating a request like POST /api/set/copt/ids ?

The Web API strictly adheres to mapping route variable names to action parameter names.

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

 [HttpPost] [ActionName("copy")] public HttpResponseMessage CopySet([FromUri(Name="id")]int[] ids) 
+5
source

In your routing table, you enable api / set / {id}. But none of your requests match this pattern.

-2
source

Source: https://habr.com/ru/post/1492820/


All Articles