通过int的名单到HTTPGET请求 [英] Passing a list of int to a HttpGet request
问题描述
我必须在结构上与此相似的功能:
I have a function similar in structure to this:
[HttpGet]
public HttpResponseMessage GetValuesForList(List<int> listOfIds)
{
/* create model */
foreach(var id in listOfIds)
model.Add(GetValueForId(id)
/* create response for model */
return response;
}
然而,当我这样做的方法GET请求:
However, when I do a Get request for the method:
{{domain}}/Controller/GetValuesForList?listOfIds=1&listOfIds=2
调试说明,当我得到一个错误, listOfIds
为空。在我们的控制,我们有许多的做工精细,并改变参数单个int它工作的时候,我已经试图改变参数类型为<$ C $公众HTTPGET方法C> INT [] 和的IEnumerable< INT方式>
过,但没有改变。
I get an error when debugging stating that listOfIds
is null. In our controller we have a number of public HttpGet methods that work fine, and when changing the parameter to a single int it works. I've tried changing the parameter type to int[]
and IEnumerable<int>
too, but no change.
但是,更改呼叫时HttpPost并通过列表作为X WWW的形式了urlencoded值,该方法的工作原理。
However, when changing the call to a HttpPost and passing the list as an x-www-form-urlencoded value, the method works.
是否有可能通过一个列出一个Get方法,否则我将不得不使用后?因为它实际上不是一个POST方法(因为它返回值的JSON模式并没有什么保存到服务器上)。
Is it possible to pass a list to a Get method, or will I have to use Post? Since it's not actually a post method (as it returns a JSON model of values and nothing is saved to the server).
推荐答案
如果您正在使用MVC的WebAPI,那么你就可以宣布你的方法是这样的:
If you are using MVC WebAPI, then you can declare your method like this:
[HttpGet]
public int GetTotalItemsInArray([FromUri]int[] listOfIds)
{
return listOfIds.Length;
}
然后您查询是这样的:
blabla.com/GetTotalItemsInArray?listOfIds=1&listOfIds=2&listOfIds=3
这将匹配数组[1,2,3 ]进入listOfIds参数(并返回3如预期)
this will match array [1, 2, 3] into listOfIds param (and return 3 as expected)
这篇关于通过int的名单到HTTPGET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!