1
votes

I have ApiController called Users And on default I have Post in there. It works fine.

However, I decided to add an additional action that also works on Post method, and now I have the following code:

[HttpPost]
public HttpResponseMessage Post(dynamic data){}

[HttpPost]
public HttpRequestMessage LogOff([FromBody]dynamic data){}

I also modified Routing, trying every commented here option: //config.Routes.MapHttpRoute( // name: "UserApi", // routeTemplate: "api/Users/LogOff/{data}/{id}", // defaults: new { id = RouteParameter.Optional } //);

//config.Routes.MapHttpRoute(
//    name: "userapi",
//    routeTemplate: "api/{controller}/{action}"
//);

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

I also played with switching their order position and no result, it always says : web api Multiple actions were found that match....

2

2 Answers

0
votes

It seems to me that the problem is: it's impossible to mix Noname actions (like standard Post) and named Post Actions. As an example I created 2 named actions which work both on Post and they work fine, however the original Post stopped working... It's weird because routing is correct and I have options for both: with and without {action}

0
votes

In the next link (http://www.codeproject.com/Articles/624180/Routing-Basics-in-ASP-NET-Web-API) you have the explanation why this is not working. I am going to show you the part that explains why your code is not working:

Unlike the familiar MVC route template, the Web API template does not specify an {action} route parameter. This is because, as we mentioned earlier, the Web API framework will by default map incoming requests to the appropriate action based upon the HTTP verb of the request.

So you cannot have two methods using the same verb (in this case POST) with the same parameters.

A solution if you want to implement it, would be using attribute routing. The next code is an example of that:

[RoutePrefix("users")]
public class UsersController : ApiController
{
    [HttpPost]
    [Route("post")]
    public HttpResponseMessage Post(dynamic data){}

    [HttpPost]
    [Route("logoff")]
    public HttpRequestMessage LogOff([FromBody]dynamic data){}

}

If you are going to use this solution, you have to add this line of code in you WebApiConfig.

config.Routes.MapHttpAttributeRoutes();

I hope that it helps.