Blankman
Blankman

Reputation: 267140

Is there a way to get all the querystring name/value pairs into a collection?

Is there a way to get all the querystring name/value pairs into a collection?

I'm looking for a built in way in .net, if not I can just split on the & and load a collection.

Upvotes: 58

Views: 86631

Answers (6)

Triloknath Nalawade
Triloknath Nalawade

Reputation: 11

Create dictionary of parameters

Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters = Request.QueryString.Keys.Cast<string>().ToDictionary(k => k, v => Request.QueryString[v]);

Upvotes: 1

M. Salah
M. Salah

Reputation: 681

You can use LINQ to create a List of anonymous objects that you can access within an array:

var qsArray = Request.QueryString.AllKeys
    .Select(key => new { Name=key.ToString(), Value=Request.QueryString[key.ToString()]})
    .ToArray();

Upvotes: 9

jishi
jishi

Reputation: 24624

If you have a querystring ONLY represented as a string, use HttpUtility.ParseQueryString to parse it into a NameValueCollection.

However, if this is part of a HttpRequest, then use the already parsed QueryString-property of your HttpRequest.

Upvotes: 6

Asad
Asad

Reputation: 21928

QueryString property in HttpRequest class is actually NameValueCollection class. All you need to do is

NameValueCollection col = Request.QueryString;

Upvotes: 2

Joel Mueller
Joel Mueller

Reputation: 28764

Well, Request.QueryString already IS a collection. Specifically, it's a NameValueCollection. If your code is running in ASP.NET, that's all you need.

So to answer your question: Yes, there is.

Upvotes: 10

Andrew Hare
Andrew Hare

Reputation: 351566

Yes, use the HttpRequest.QueryString collection:

Gets the collection of HTTP query string variables.

You can use it like this:

foreach (String key in Request.QueryString.AllKeys)
{
    Response.Write("Key: " + key + " Value: " + Request.QueryString[key]);
}

Upvotes: 103

Related Questions