ApiController行动未能从查询字符串数组解析 [英] ApiController Action Failing to parse array from querystring
问题描述
使用Visual Studio 2012.2,MVC4 Web应用程序。
Using Visual Studio 2012.2, MVC4 web application.
我已经要求来我ApiController像这样:
I have requests coming to my ApiController like so:
http://localhost/api/keys?ids[]=1&ids[]=2&ids[]=3
和我是IM pression下,下面的方法应该能够自动地从IDS检索值[]数组:
And I was under the impression that the following method should be able to automagically retrieve the values from the ids[] array:
public KeysModel Get(int[] ids){...}
然而,当像上面这样一个请求时,在IDS的值参数为null。
However, when a request like the one above comes in, the value of the "ids" parameter is null.
我已经考察了HttpContext.Current.Request.QueryString对IDS的价值观,我能得到他们的方式,但是这使得单元测试更难了。
I have inspected that HttpContext.Current.Request.QueryString has the values for ids, and I could get to them that way, but that makes unit testing harder.
我使用列表ID也尝试过,[FromUri],[FromUri(NAME =IDS []),对象ID和字符串ID(有趣的注意......当IDS是一个字符串变量,在值它是(集合)
I have also tried using List ids, [FromUri], [FromUri(Name="ids[]")], object ids, and string ids (interesting note... when ids is a string variable, the value within it is "(Collection)"
推荐答案
原来
public KeysModel Get([FromUri]int[] ids){...}
毕竟他回答。
不知道我在做之前...
Dunno what I was doing before...
这篇关于ApiController行动未能从查询字符串数组解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!