I'm using Web API (part of ASP.NET MVC 5), and I'm trying to bind querystring values to a Dictionary<int, bool>
.
My Web API method is simply:
[HttpGet]
[Route("api/items")]
public IQueryable<Item> GetItems(Dictionary<int, bool> cf)
{
// ...
}
If I try to invoke this URL:
/api/items?cf[1009]=true&cf[1011]=true&cf[1012]=false&cf[1015]=true
The parameter cf
is always null
.
How can I pass in a dictionary of values via the QueryString to a Web API method?
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
In my Action method signature, parameters is defined as:
Dictionary<string,string> parameters
Hope this helps.
I was able to figure this our myself after reading this post from Scott Hanselman. So thanks to Scott!!
Thanks, Nirav
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With