I am trying to create a MVC WebAPI controller, which takes in an id, which it creates a record with in the database and then returns. However, I keep getting an error.
In my testAPI controller I have:
[HttpPost]
public HttpResponseMessage OpenSession(int id)
{
//Logic of post in here never gets hit
}
However when I try and post to the API I get the following response:
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:54388/api/testAPI/OpenSession/'.","MessageDetail":"No action was found on the controller 'testAPI' that matches the request."}
I have changed my routing to:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/"
);
I am trying to post to: http://localhost:54388/api/testAPI/OpenSession/
with the value id in the payload. However I think it is expecting it in the URL - can someone please point out where I am going wrong.
WebApi is problematic when you try to POST only a single parameter. I remember I had the same problem. There are many ways to sort this out, but the one that works every time is to have a model instead of an int:
Or if you insist on not having a class, you can try this: http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/
I have used this for a bit, but ended up replacing all of
[FromBody]
arguments to models - works more reliable.