简体   繁体   English

ASP .NET Web API-在POST方法中获取纯json

[英]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. 我有一个具有POST方法的Web API控制器,该方法从请求主体接收数据,并且它所做的只是将其发送到另一个Web服务,因此不需要反序列化数据。 How can I disable the auto deserialization done by the Web API? 如何禁用由Web API完成的自动反序列化?

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

}

The data arrives as null with this signature. 带有此签名的数据将为空。

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 有关该解决方案的更多信息: http : //bizcoder.com/posting-raw-json-to-web-api

Send the object as string (in its serialized format). 将对象作为字符串发送(以其序列化格式)。 For example 例如

YourMethod([FromBody]string json)

Just read the reuest content like this: 只需阅读以下内容即可:

public Task<IHttpActionResult> Post() {
    var str = await Request.Content.ReadAsStringAsync();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM