Is it possible for the Web API to map values from a message body to the argument list? For example, if I had this:
public HttpResponseMessage Post(int id, string value1, string value2) {}
instead of this:
public HttpResponseMessage Post(MyCustomClass message) {}
I was posting JSON via fiddler and could only get the latter to work. Although I much prefer the latter, I was just curious if it were possible to get the first example to work. I could get it to work using querystring params, but I couldn’t get it to work posting form values or json. Just curious.
No it’s not possible. Mike Stall has a great post explaining this in details – http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx
In short, in MVC you only had model binders, and the entire request body was buffered and fed into the binder.
In Web API you have both model binders and formatters.
Rule of thumb is that Web API uses model binders to read querystring parameters and formatters to read the body. And the request body is not buffered – it’s a read-only, one directional, async stream. Therefore you can only read one thing from the body – whether it is a simple type (i.e. string, int) or complex type, doesn’t matter – it can only be one thing.