[英]C# / ASP.NET Core Web API : how to pass a list parameter?
我正在尝试将列表参数传递给 ASP.NET Core 5 Web API。
这是我的端点:
[ApiController]
[Route("[controller]")]
public class InvoiceController : ControllerBase
{
private readonly IConfiguration _configuration;
public InvoiceController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpPost]
public List<CustomerInvoice> Post(string[] CustomerNumbers, DateTime OrderDateFrom, DateTime OrderDateTo)
{
}
}
发送 json 发布请求失败并出现以下错误:
JSON 值无法转换为 System.String[]。 路径:$ | 行号:0 | 字节位置内线:1。
这是请求:
{
"CustomerNumbers": ["test"],
"OrderDateFrom": "2021-01-01",
"OrderDateTo": "2021-11-02"
}
我也尝试了List<string>
,而不是使用字符串数组。 我得到了同样的错误。
知道为什么框架不了解如何接收列表吗?
由于您的参数名称,我想您的数组看起来像这样[2, 3, 5]
但应该看起来像这样["2", "3", "5"]
。 它需要一个字符串数组,而不是一个 int 数组。 否则将数据类型更改为int[] CustomerNumbers
您应该创建一个 class 并使用此 class 接收 json。 这可能会解决您的问题。
public class ClassExample
{
public List<string> CustomerNumbers { get; set; }
public DateTime OrderDateFrom { get; set; }
public DateTime OrderDateTo { get; set; }
}
[HttpPost]
public List<CustomerInvoice> Post(ClassExample requestParameter)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.