【发布时间】:2018-03-31 16:05:15
【问题描述】:
我想将一组 ID 传递给控制器。最初我在查询字符串中添加每个 ID,如下所示:
http://localhost:4000/customers/active?customerId=1&customerId=2&customerId=3
然后在控制器端我有一个方法可以接受这样的数组:
GetCustomers([FromQuery] int[] ids)
{
...
}
这运行良好,但在某些情况下,数组中有太多 customerIds,导致查询字符串变得太长,因此我不得不修改将查询传递给它的方式:
http://localhost:4000/customers/active?customerIds=1,2,3
我通过更改 GetCustomers 参数以接受字符串而不是 int 数组,然后在控制器中解析 customerIds (使用 .Split(',')),得到了解决方案
我觉得直接传递数组而不是在服务器端修改字符串更简洁。考虑到customerIds 现在的传递方式,有没有办法实现这一点?
【问题讨论】:
-
操作是 POST 还是 GET?
-
@Nkosi 这是一个 GET
标签: c# asp.net-mvc asp.net-core controller asp.net-mvc-routing