Posted by Anuraj on Tuesday, November 5, 2013
Most of the time while working with Web API; we were dealing with particular model from our domain and creating GET/POST/PUT/DELETE methods that map to CRUD operations. But today I faced an issue, where I need to store some data, and I don’t have a mapping class for the same. As WebAPI doesn’t support two classes in Post action using FromBody attribute, I did it by combining FromBody and FromUri attributes, like this.
And you can do post like this using fiddler.
It works, but it exposes the class properties in the url, as your model class properties increases, length of the POST url also will get increase. I don’t think it is good solution. Then I found an alternate solution using dynamic type. To use dynamic type, you can modify the action method signature like this, which will accept anything.
And you can post from fiddler like this.
In controller action method you can access the classes and properties like this.
You can also return the dynamic type from a Web API method.