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

C#asp.netCollectionsQuery String

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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]);
}

Solution 2 - C#

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.

Solution 3 - C#

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();

Solution 4 - C#

If you have a querystring ONLY represented as a string, use http://msdn.microsoft.com/en-us/library/system.web.httputility.parsequerystring.aspx">HttpUtility.ParseQueryString</a> to parse it into a NameValueCollection.

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

Solution 5 - C#

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

> NameValueCollection col = > Request.QueryString;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBlankmanView Question on Stackoverflow
Solution 1 - C#Andrew HareView Answer on Stackoverflow
Solution 2 - C#Joel MuellerView Answer on Stackoverflow
Solution 3 - C#M. SalahView Answer on Stackoverflow
Solution 4 - C#jishiView Answer on Stackoverflow
Solution 5 - C#AsadView Answer on Stackoverflow