Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my POST actions not found in ASP.NET Web API?

This is my DefaultApi configuration:

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

[ActionName("DefaultAction")]
public HttpResponseMessage Get(string id)

[ActionName("DefaultAction")]
public HttpResponseMessage Post(MyClass obj)

How come GET works but using POST I get a 404 Not Found error?

Any ideas or suggestions?

Edit:

Client JavaScript:

$.ajax({
    type: "POST",
    url: "@Url.Content("~/api/controllername")",
    data: args,
    200: function (data) {
          ......
        }
});
like image 613
Valter Avatar asked Sep 16 '13 23:09

Valter


1 Answers

I had a similar problem and the solution was to add [FromBody] to the method's paramter like this:

[HttpPost]
public void PostIncreaseViewCounter([FromBody]int id)
{
}
like image 52
Krisztián Balla Avatar answered Sep 23 '22 20:09

Krisztián Balla