NameValueCollection for editing query strings

If i call

var nvc = HttpUtility.ParseQueryString("?foo=bar&baz=robots")

I am returning a NameValueCollection where, if I call ToString on it, I am returning a query string.

var str = nvc.ToString(); //foo=bar&baz=robots....

If I create a new NameValueCollection, add material to it and call ToString () on it, I will not return the query string.

var nvc= new NameValueCollection();
nvc["foo"] = "bar";
var str = nvc.ToString(); //default for Object.ToString()

There is also no way to build a NameValueCollection that acts as a query string editor. Is there any? If not, why? The ability to edit query strings is a pretty useful thing, but this functionality is completely hidden in obscure mode of some object that most people don’t even know.

+2
source share
2 answers

HttpValueCollection, NameValueCollection ToString().
ParseQueryString() - .

+2

. , - :

Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("somekey", "someval");
var querystring = string.Join("&", dict.Select(kv => HttpUtility.UrlEncode(kv.Key) + "=" + HttpUtility.UrlEncode(kv.Value)));

, . , - = , . - ?

0

All Articles