Skip to content

Instantly share code, notes, and snippets.

@mawiseman
Last active August 9, 2017 05:01
Show Gist options
  • Save mawiseman/14ed93d3c368013956f0728bbe5a350f to your computer and use it in GitHub Desktop.
Save mawiseman/14ed93d3c368013956f0728bbe5a350f to your computer and use it in GitHub Desktop.
using System.Collections.Specialized;
using System.Linq;
using System.Web;
public static class NameValueCollectionExtensions
{
public static string ToQueryString(this NameValueCollection nvc)
{
return string.Join("&",
nvc.AllKeys.Where(key => !string.IsNullOrWhiteSpace(nvc[key]))
.Select(
key => string.Join("&", nvc.GetValues(key).Select(val => string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(val))))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment