【发布时间】:2021-08-27 01:36:30
【问题描述】:
我以前使用Controller而不是ApiController,今天我尝试使用ApiController,发现下面的问题。
ASP.NET Core Web Api HttpPost Action 参数无法接收 axios application/json post 传递数据
asp.net核心api代码
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> Post(string json)
{
return this.Ok();
}
}
前端
var json = '{"json_data":"{\"value\":\"hello world\"}';
axios.post('Test', {"json": json})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
实际要求
Accept:application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Content-Type: application/json;charset=UTF-8
Request Payload :
{"json_data":"{\"value\":\"hello world\"}
实际动作json参数数据为null
预期接收数据:{"json_data":"{\"value\":\"hello world\"}
更新
我尝试了下面的代码,我得到 rawValue value = {"json_data":"{\"value\":\"hello world\"} 但 json value = null
[HttpPost]
public async Task<IActionResult> Post(string json)
{
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
string rawValue = await reader.ReadToEndAsync();
}
}
【问题讨论】:
标签: c# .net asp.net-core asp.net-web-api asp.net-core-3.1