12

.Net's System.Web.HttpUtility class defines the following function to parse a query string into a NameValueCollection:

public static NameValueCollection ParseQueryString(string query);

Is there any function to do the reverse (i.e. to convert a NameValueCollection into a query string)?

Alconja
  • 14,834
  • 3
  • 60
  • 61
Palani
  • 8,962
  • 11
  • 53
  • 62
  • 1
    Similar Questions http://stackoverflow.com/questions/229925/outputing-a-manipulated-querystring-in-c/ http://stackoverflow.com/questions/829080/how-to-build-a-query-string-for-a-url-in-c – Palani Apr 09 '10 at 06:15

5 Answers5

25

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"

Ivan Hamilton
  • 3,205
  • 1
  • 23
  • 14
4

A NameValueCollection has an automatic ToString() method that will write all your elements out as a querystring automatically.

you don't need to write your own.

var querystringCollection = HttpUtility.ParseQueryString("test=value1&test=value2");

var output = querystringCollection.ToString(); 

output = "test=value1&test=value2"

Doug
  • 6,460
  • 5
  • 59
  • 83
  • 11
    Incorrect. "System.Collections.Specialized.NameValueCollection" does NOT support this. It's a feature of "System.Web.HttpValueCollection". – Ivan Hamilton Dec 22 '11 at 10:58
  • 1
    ... and "System.Web.HttpValueCollection" is an internal type. – Scott Aug 20 '12 at 18:08
  • 1
    Note that this does not handle international characters; it uses HttpUtility.UrlEncodeUnicode internally to escape characters. – Rune Sep 20 '13 at 15:33
3

I found that a combination of UriBuilder and HttpUtility classes meets my requirements to manipulate query parameters. The Uri class on its own is not enough, particularly as its Query property is read only.

var uriBuilder = new UriBuilder("http://example.com/something?param1=whatever");
var queryParameters = HttpUtility.ParseQueryString(uriBuilder.Query);
queryParameters.Add("param2", "whatever2");
queryParameters.Add("param3", "whatever2");
uriBuilder.Query = queryParameters.ToString();
var urlString = uriBuilder.Uri.ToString();

The above code results in the URL string: http://example.com/something?param1=whatever&param2=whatever2&param3=whatever2

Note that the ToString() goes via a Uri property, otherwise the output string would have an explicit port 80 in it.

It's nice to be able to do all this using framework classes and not have to write our own code.

NickBeaugié
  • 720
  • 9
  • 17
2

I don't think there is a built in one, but here is an example of how to implement http://blog.leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/

Jonathan
  • 5,953
  • 1
  • 26
  • 35
-1

Here are 2 very useful functions that I use all the time:

    private string GetQueryStringParameterValue(Uri url, string key)
    {
        return HttpUtility.ParseQueryString(url.Query.TrimStart('?'))[key];
    }

    private Uri SetQueryStringParameterValue(Uri url, string key, string value)
    {
        var parameters = HttpUtility.ParseQueryString(url.Query.TrimStart('?'));

        parameters[key] = value;

        var uriBuilder = new UriBuilder(url) { Query = parameters.ToString() };

        return uriBuilder.Uri;
    }
skb
  • 30,624
  • 33
  • 94
  • 146