Cher
Cher

Reputation: 2947

How to get every parameters and their value?

I use the following function:

HttpUtility.ParseQueryString

I'm able to extract one parameter value so far. However now I'd like if possible to obtain all parameters in an array, and another array with all their value. Is there a way to do this?

For one parameter value I do this:

HttpUtility.ParseQueryString(myUri.Query).Get(param)

In the worst case, if I can't have an array for both, if I could get a string array for the parameters, I could then use the line above with every parameters to get the value.

Upvotes: 0

Views: 692

Answers (2)

Dmitry Polishuk
Dmitry Polishuk

Reputation: 196

You can get dictionary of parameters and then get each parameter value.

string uri = "http://www.example.com?param1=good&param2=bad";
string queryString = new System.Uri(uri).Query;

var queryDictionary = System.Web.HttpUtility
                            .ParseQueryString(queryString);

var paramsList = new Dictionary<string, string>();

foreach (var parameter in queryDictionary)
{
     var key = (string) parameter;
     var value = queryDictionary.Get(key);
     paramsList.Add(key, value);
}

Upvotes: 2

Vir
Vir

Reputation: 652

Taken from documentation:

NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);

StringBuilder sb = new StringBuilder("<br />");
foreach (String s in qscoll.AllKeys)
{
    sb.Append(s + " - " + qscoll[s] + "<br />");
}

ParseOutput.Text = sb.ToString();

So the result of ParseQueryString is a Dictionary, which holds all parsed data, just iterate through it :)

Upvotes: 1

Related Questions