1

I don't know what they are called, that's why I can't find a solution. I have a querystring looks like an array

params[name]=john&params[age]=25&params[country]=ru

Is there a way to parse these parameters as string[] or Dictionary<string, string>?

UPD: On php this type of query string is being parsed automaticaly using

$params = $_GET['params']; $params['name']

But I can't find an equivalent on C#

MG_REX
  • 21
  • 1
  • 5

1 Answers1

4

If you're using the ASP.NET MVC Framework, the default parameter binder that converts query string values into typed parameters can deal with it automatically in the following way:

One-Dimensional Arrays

// GetData?filters[0]=v1&filters[1]=v2&filters[3]=v3
public ActionResult GetData(IEnumerable<string> filters) 
{
    // todo
}

Two-Dimensional Arrays

// GetData?filters[0][field]=name&filters[0][type]=1&filters[0][value]=val
public ActionResult GetData(IEnumerable<Dictionary<string,string>> filters) 
{
    // todo
}

... and so on.

For more info and examples, check out this blog post that I wrote on this topic.

Darkseal
  • 9,205
  • 8
  • 78
  • 111