[英]What is the ASP.NET Core equivalent to HttpRequestMessage?
I found a blog post that shows how POSTed JSON can be received as a string. 我发现了一篇博客文章 ,展示了如何以字符串形式接收POSTed JSON。
I want to know what's the new native way to do the same thing as the following code in a REST Post method in a Controller: 我想知道在Controller中的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);
}
The other option bellow does not work for me, i think because I don't use Content-Type: application/json
in the request header (can't change this), and I get a 415 . 下面的另一个选项对我不起作用,我认为因为我在请求标题中不使用
Content-Type: application/json
(不能改变它),我得到415。
public HttpResponseMessage Post([FromBody]JToken jsonbody)
{
// Process the jsonbody
return new HttpResponseMessage(HttpStatusCode.Created);
}
In .Net Core they have merged the Web API and MVC so you could just do it like this with IActionResult
or one of its derivatives. 在.Net Core中,他们已经合并了Web API和MVC,因此您可以使用
IActionResult
或其衍生产品进行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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.