【发布时间】:2018-01-16 00:39:06
【问题描述】:
我找到了一个blog post,它显示了如何将 POST 的 JSON 作为字符串接收。
我想知道在控制器的 REST Post 方法中执行与以下代码相同的操作的新本地方法是什么:
public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
var jsonString = await request.Content.ReadAsStringAsync();
// Do something with the string
return new HttpResponseMessage(HttpStatusCode.Created);
}
下面的另一个选项对我不起作用,我想是因为我没有在请求标头中使用 Content-Type: application/json (无法更改),我得到了 415 。
public HttpResponseMessage Post([FromBody]JToken jsonbody)
{
// Process the jsonbody
return new HttpResponseMessage(HttpStatusCode.Created);
}
【问题讨论】:
标签: c# json rest asp.net-core asp.net-core-webapi