MG_REX
MG_REX

Reputation: 21

ASP.NET GET parameters with square brackets

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#

Upvotes: 1

Views: 2957

Answers (1)

Darkseal
Darkseal

Reputation: 9564

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.

Upvotes: 4

Related Questions