将查询字符串值绑定到字典 [英] Binding querystring values to a dictionary
问题描述
我正在使用Web API(ASP.NET MVC 5的一部分),并且试图将查询字符串值绑定到 Dictionary< int,bool>
.
I'm using Web API (part of ASP.NET MVC 5), and I'm trying to bind querystring values to a Dictionary<int, bool>
.
我的Web API方法很简单:
My Web API method is simply:
[HttpGet]
[Route("api/items")]
public IQueryable<Item> GetItems(Dictionary<int, bool> cf)
{
// ...
}
如果我尝试调用此URL:
If I try to invoke this URL:
/api/items?cf[1009]=true&cf[1011]=true&cf[1012]=false&cf[1015]=true
参数 cf
始终为 null
.
如何通过QueryString将值的字典传递给Web API方法?
How can I pass in a dictionary of values via the QueryString to a Web API method?
推荐答案
当您要使用查询字符串参数传递字典(主要是键和值对)时,应使用以下格式:& parameters [0] .key = keyName& parameters [0] .value = keyValue
When you want to pass a dictionary (basically key and value pair) using a query string parameter then you should use format like:
¶meters[0].key=keyName¶meters[0].value=keyValue
在我的Action方法签名中,参数定义为:
In my Action method signature, parameters is defined as:
Dictionary<string,string> parameters
希望这会有所帮助.
在阅读Scott Hanselman的这篇文章之后,我自己弄清楚了这一点.所以,谢谢斯科特!
I was able to figure this our myself after reading this post from Scott Hanselman. So thanks to Scott!!
谢谢,尼拉夫
这篇关于将查询字符串值绑定到字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!