c# rest api 只获取第一个值参数列表

c# rest api only getting first value parameter list

我正在寻找一种在 C# 中制作我的 REST Api 的方法,以便它能够处理这样的请求:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

现在我的控制器里有这个:

[HttpGet]
[Microsoft.AspNetCore.Mvc.ProducesResponseType(typeof(Order), 200)]
[Route("api/Order/GetWithParam")]
public List<Order> GetDataWithParam(string orderIdString = null, DateTime? startDate = null, DateTime? endDate = null, string orderDescriptorsIdString = null)
    {
      ...

如果我的参数有一个或零个自变量,这会很好用:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4

orderIdString 将等于“4”

但是如果我为一个参数发送多个参数,则只会采用第一个参数:

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

orderIdString 将等于“4”...但它应该等于“4,7”。

我错过了什么?

-- 编辑--

我试过改:

string orderIdString = null

string[] orderIdString

但现在即使传递了参数,orderIdString 也为 null

http://localhost:1234/api/Order/GetWithParam?orderIdString=4&orderIdString=7

会给我 orderIdString == null

如果您想绑定多个参数值,您应该将类​​型设为数组 (string[]):

[HttpGet]
[Microsoft.AspNetCore.Mvc.ProducesResponseType(typeof(Order), 200)]
[Route("api/Order/GetWithParam")]
public List<Order> GetDataWithParam([FromQuery]string[] orderIdString = null, DateTime? startDate = null, DateTime? endDate = null, string orderDescriptorsIdString = null)
    {
      ...