How can I get the original JSON dictionary from an ASP.NET MVC Web API message?

With a subclass of ApiController, it has the ability in the Post method to bind it to an existing model object, for example

public class RegisterController : ApiController
{
    public void Post(Product product)

but if the input JSON data contains data that I will use to create several model objects, how can I directly get the data?

    public void Post(dynamic value)

returns value as null. Is there an easy shortcut way to get to it, like request.POST ['name'] or something else?

Say the data looks like

{
    'productID':1,
    'productName':'hello',
    'manufacturerID':1,
    'manufacturerName':'world'
}
+3
source share
1 answer

One of the options may use one of the ReadAsAsync * methods in the HttpContent instance from the Request object

public void Post() {
   var result = this.Request.Content.ReadAsAsync<string>().Result;
}

, , .

...

public void Post(IEnumberable<Product> products) {

}
+6

All Articles