gpt4 book ai didi

C# .net 核心 web api post 参数始终为 null

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:18 26 4
gpt4 key购买 nike

所以这让我抓狂。我正在做一些非常简单的事情,向我的 web api 发送 POST 请求。端点设置如下:

[HttpPost]
[Route("locations")]
public async Task<IActionResult> PostLocations([FromBody]IEnumerable<Location>locations)

我正在按如下方式调用电话:

http://localhost:60254/api/Fetch/locations
With the body
{
"Locations": [
{
"LocationId": "111",
"ProductId": 110,
"Sku": "11131-LJK"
}
]
}

And header: content-type: application/json

现在又一次,这是非常简单的东西,应该开箱即用,这个该死的框架变化把一切都搞砸了。

现在,如果我获取 HttpContext 并直接读取正文流

    using (StreamReader reader = new StreamReader(HttpContext.Request.Body, Encoding.UTF8))
{
string body = reader.ReadToEnd();
}

我可以看到正文被正确发送,我有一个格式非常好的 json,我可以将其转换成任何我想要的。所以问题是我错过了什么这个端点不起作用?

web api 项目模板没有立即添加哪些配置才能使其正常工作?

最佳答案

您的有效负载不是 Location 的列表,而是具有 Locations 属性的对象,该属性是一个列表。

代替

{
"Locations": [
{
"LocationId": "111",
"ProductId": 110,
"Sku": "11131-LJK"
}
]
}

使用

 [
{
"LocationId": "111",
"ProductId": 110,
"Sku": "11131-LJK"
}
]

关于C# .net 核心 web api post 参数始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com