C# ApicController操作无法从querystring解析数组

C# ApicController操作无法从querystring解析数组,c#,asp.net-mvc-4,asp.net-web-api,C#,Asp.net Mvc 4,Asp.net Web Api,使用Visual Studio 2012.2,MVC4 web应用程序 我的ApiController收到如下请求: http://localhost/api/keys?ids[]=1&ids[]=2&ids[]=3 我的印象是,以下方法应该能够自动从ids[]数组中检索值: public KeysModel Get(int[] ids){...} 然而,当像上面这样的请求传入时,“ids”参数的值为null 我已经检查了HttpContext.Current.Request

使用Visual Studio 2012.2,MVC4 web应用程序

我的ApiController收到如下请求:

http://localhost/api/keys?ids[]=1&ids[]=2&ids[]=3
我的印象是,以下方法应该能够自动从ids[]数组中检索值:

public KeysModel Get(int[] ids){...}
然而,当像上面这样的请求传入时,“ids”参数的值为null

我已经检查了HttpContext.Current.Request.QueryString是否具有ID的值,我可以通过这种方式获得这些值,但这使得单元测试更加困难

我还尝试过使用列表ID、[FromUri]、[FromUri(Name=“ids[]”)、对象ID和字符串ID(有趣的注意……当ID是字符串变量时,其中的值是“(Collection)”

<

public KeysModel Get([FromUri]int[] ids){...}
这毕竟是答案


不知道我以前在做什么…

你能试着把你的uri改成这样,看看它是否有效:
http://localhost/api/keys?ids=1&ids=2&ids=3
并且您还应该用
[FromUri]装饰参数
属性,默认情况下,Web API认为复杂类型来自Body无法更改uri。它是以这种方式来自ember的,目标是不制作自定义适配器/序列化程序,而是服从ember的意愿。结果证明我未能正确使用[FromUri](我确实认为……一定是“盯着它看得太久了”问题)。不要在查询字符串中使用
[]
,我相信这是PHP特有的习惯用法。不要在查询字符串中多次使用同一个键,这太愚蠢了!