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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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]);
}
Method 2
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.
Method 3
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();
Method 4
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.
Method 5
QueryString property in HttpRequest class is actually NameValueCollection class. All you need to do is
NameValueCollection col =
Request.QueryString;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0