Ger*_*hes 8 c# rest json asp.net-core asp.net-core-webapi
我找到了一篇博客文章,该文章显示了如何以字符串形式接收POSTed 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);
}
Run Code Online (Sandbox Code Playgroud)
下面的另一个选项对我不起作用,我想是因为我没有Content-Type: application/json在请求标头中使用(无法更改它),并且得到415。
public HttpResponseMessage Post([FromBody]JToken jsonbody)
{
// Process the jsonbody
return new HttpResponseMessage(HttpStatusCode.Created);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
在.Net Core中,他们将Web API和MVC合并在一起,因此您可以使用它IActionResult或其中之一来完成此操作。
public IActionResult Post([FromBody]JToken jsonbody)
{
// Process the jsonbody
return Created("", null);// pass the url and the object if you want to return them back or you could just leave the url empty and pass a null object
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5012 次 |
| 最近记录: |