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.
Yes, use the HttpRequest.QueryString collection:
HttpRequest.QueryString
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]); }
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.
Request.QueryString
NameValueCollection
So, to answer your question: Yes, there is.
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.
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();
QueryString property in the HttpRequest actually the NameValueCollection class. All you have to do is
QueryString
HttpRequest
NameValueCollection col = Request.QueryString;