无法将整数集合发送到 Web Core Api Post 方法,它被设置为 null

Cannot send a collection of integers to a Web Core Api Post method, it is set to null

我想将整数集合发送到 Web 核心 api 上的 post 方法。

方法是;

[HttpPost("open")]
public IActionResult OpenInspections([FromBody]IEnumerable<int> inspectionIds)
{
    return NoContent();
//...

这只是为了测试,我在return语句上打了一个断点,inspectionIds有效载荷是null.

在 Postman 中我有

编辑:我刚刚从签名中删除了方括号。我尝试了 IEnumerable<int>int[] 但都没有用

为空,因为发布的内容和动作期望的内容不匹配,所以发布时不绑定模型。发送的示例数据具有 string 数组 ["11111111", "11111112"] 而不是 int 数组 [11111111, 11111112]

IEnumerable<int>[]代表集合的集合,像

{ "inspectionIds": [[11111111, 11111112], [11111111, 11111112]]}

要获得所需的行为,请更新操作以期望所需的数据类型

[HttpPost("open")]
public IActionResult OpenInspections([FromBody]int[] inspectionIds) {
    //...
}

确保发布的正文也符合预期

[11111111, 11111112]

考虑使用具体模型,因为所提供问题中的发布数据是 JSON object.

public class Inspection {
    public int[] inspectionIds { get; set; }
}

并相应地更新操作

[HttpPost("open")]
public IActionResult OpenInspections([FromBody]Inspection model) {
    int[] inspectionIds = model.inspectionIds;
   //...
}

该模型还必须与发布的预期数据相匹配。

{ "inspectionIds": [11111111, 11111112] }

请注意,如果所需的 ID 假定为 int,则不要将它们括在引号中。

我认为问题出在这里:IEnumerable<int>[] - 它是整数列表的数组?

应该很简单int[](或者可以是IEnumerable<int>)。