System.Collections.Specialized.NameValueCollection
does NOT support this, but a derived internal class System.Web.HttpValueCollection
DOES (by overriding ToString()
).
Unfortunately (being internal) you cannot instantiate this class directly, but one is returned by HttpUtility.ParseQueryString()
(and you can call this with String.Empty
, but not Null
).
Once you have a HttpValueCollection
, you can fill it from your original NameValueCollection
by calling Add()
, before finally calling ToString()
.
var nameValueCollection = new NameValueCollection {{"a","b"},{"c","d"}};
var httpValueCollection = System.Web.HttpUtility.ParseQueryString(String.Empty);
httpValueCollection.Add(nameValueCollection);
var qs = httpValueCollection.ToString();
nameValueCollection.ToString() = "System.Collections.Specialized.NameValueCollection"
httpValueCollection.ToString() = "a=b&c=d"