Reverse function of HttpUtility.ParseQueryString
Asked Answered
T

5

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)?

Tabernacle answered 9/4, 2010 at 5:30 Comment(1)
Similar Questions #230425 #829580Tabernacle
M
3

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"

Moly answered 25/3, 2011 at 4:17 Comment(3)
Incorrect. "System.Collections.Specialized.NameValueCollection" does NOT support this. It's a feature of "System.Web.HttpValueCollection".Ninebark
... and "System.Web.HttpValueCollection" is an internal type.Bastien
Note that this does not handle international characters; it uses HttpUtility.UrlEncodeUnicode internally to escape characters.Amerigo
N
26

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"

Ninebark answered 22/12, 2011 at 10:57 Comment(0)
M
3

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"

Moly answered 25/3, 2011 at 4:17 Comment(3)
Incorrect. "System.Collections.Specialized.NameValueCollection" does NOT support this. It's a feature of "System.Web.HttpValueCollection".Ninebark
... and "System.Web.HttpValueCollection" is an internal type.Bastien
Note that this does not handle international characters; it uses HttpUtility.UrlEncodeUnicode internally to escape characters.Amerigo
P
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.

Priscillaprise answered 19/12, 2014 at 11:46 Comment(0)
S
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/

Schreibe answered 9/4, 2010 at 5:59 Comment(0)
R
-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;
    }
Reluctivity answered 25/8, 2012 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.