Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding querystring values to a dictionary

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?

like image 427
qJake Avatar asked Mar 28 '15 17:03

qJake


1 Answers

When you want to pass a dictionary (basically key and value pair) using a query string parameter then you should use format like: &parameters[0].key=keyName&parameters[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

like image 65
Nirav Darji Avatar answered Sep 23 '22 19:09

Nirav Darji