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

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

I am looking for a built-in way in .net, if not, I can just split into and and load the collection.

+43
collections c # query-string
Mar 03
source share
5 answers

Yes, use the HttpRequest.QueryString collection:

Gets the set of HTTP request string variables.

You can use it as follows:

 foreach (String key in Request.QueryString.AllKeys) { Response.Write("Key: " + key + " Value: " + Request.QueryString[key]); } 
+77
Mar 03 '10 at 22:09
source share
β€” -

Well, Request.QueryString already has a collection. In particular, this is a NameValueCollection . If your code works in ASP.NET, that’s all you need.

So, to answer your question: Yes, there is.

+9
Mar 03
source share

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

However, if this is part of the HttpRequest, use the already processed QueryString property of your HttpRequest.

+6
Mar 03 '10 at 22:17
source share

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

 var qsArray = Request.QueryString.AllKeys .Select(key => new { Name=key.ToString(), Value=Request.QueryString[key.ToString()]}) .ToArray(); 
+4
Dec 15 '15 at 9:02
source share

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

NameValueCollection col = Request.QueryString;

+2
Mar 03 '10 at 22:11
source share



All Articles