user6251216 user6251216 - 3 months ago 18
ASP.NET (C#) Question

ASP .NET Web API - Get plain json in POST method

I have a Web API controller that has a POST method which receives data from the request body, and all it does is send it to another web service, so deserializing the data is not necessary. How can I disable the auto deserialization done by the Web API?

public IHttpActionResult Post([FromBody]string data)
{
//Post with http client...

}


The data arrives as null with this signature.

Answer

Check this:

public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
    var data = await request.Content.ReadAsStringAsync();
    // do stuff with the content
}

More about the solution: http://bizcoder.com/posting-raw-json-to-web-api