在 ASP.NET WEB API 中处理 POST 请求时的不同值
Different value when processing POST request in ASP.NET WEB API
我有以下情况,以前从未见过。我正在使用下面的代码来声明一个 Post 动作。
[HttpPost]
[Route("")]
public async Task<HttpResponseMessage> Insert(InsertRequest request)
{
var body = await Request.Content.ReadAsStringAsync();
}
现在,当我使用 Content-Type = Application/Json 的 Postman 向此端点发送请求时,我得到一些请求值和正文的空字符串。
如果我对这个端点使用 HttpClient 执行 PostAsJsonAsync,我将得到 null 的请求和正文的请求内容。
这怎么可能?
要支持 POST
,您需要将属性 [FromBody]
添加到请求参数。
[HttpPost]
[Route("")]
public async Task<HttpResponseMessage> Insert([FromBody] InsertRequest request)
{
var body = await Request.Content.ReadAsStringAsync();
}
我有以下情况,以前从未见过。我正在使用下面的代码来声明一个 Post 动作。
[HttpPost]
[Route("")]
public async Task<HttpResponseMessage> Insert(InsertRequest request)
{
var body = await Request.Content.ReadAsStringAsync();
}
现在,当我使用 Content-Type = Application/Json 的 Postman 向此端点发送请求时,我得到一些请求值和正文的空字符串。
如果我对这个端点使用 HttpClient 执行 PostAsJsonAsync,我将得到 null 的请求和正文的请求内容。
这怎么可能?
要支持 POST
,您需要将属性 [FromBody]
添加到请求参数。
[HttpPost]
[Route("")]
public async Task<HttpResponseMessage> Insert([FromBody] InsertRequest request)
{
var body = await Request.Content.ReadAsStringAsync();
}