views:

139

answers:

4

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.

+7  A: 

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]);
}
Andrew Hare
+3  A: 

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.

Joel Mueller
A: 

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

NameValueCollection col = Request.QueryString;

Asad Butt
A: 

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.

jishi