Thursday, August 27, 2009

NameValueCollection to Query String

The HttpUtility.ParseQueryString method can be used to convert a query string to a NameValueCollection.

But there doesnt seem to be a method to convert a NameValueCollection to a Query String.

So here is a quick helper method to do just that:

static String ConstructQueryString(NameValueCollection parameters)
{
    List<String> items = new List<String>();
    foreach (String name in parameters)
        items.Add(String.Concat(name, "=", System.Web.HttpUtility.UrlEncode(parameters[name])));

    return String.Join("&", items.ToArray());
}

No comments: