我找到了一个 blog post这显示了如何将 POSTed JSON 作为字符串接收。
我想知道在 Controller 的 REST Post 方法中执行与以下代码相同的事情的新 native 方法是什么:
public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
var jsonString = await request.Content.ReadAsStringAsync();
// Do something with the string
return new HttpResponseMessage(HttpStatusCode.Created);
}
下面的另一个选项对我不起作用,我想是因为我没有在请求 header 中使用 Content-Type: application/json
(无法更改),我得到一个 415。
public HttpResponseMessage Post([FromBody]JToken jsonbody)
{
// Process the jsonbody
return new HttpResponseMessage(HttpStatusCode.Created);
}
最佳答案
在 .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
}
关于c# - ASP.NET Core 中的 HttpRequestMessage 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45568380/