public static string ToQueryString(NameValueCollection collection, bool startWithQuestionMark = true)
{
if (collection == null || !collection.HasKeys())
return String.Empty;
var sb = new StringBuilder();
if (startWithQuestionMark)
sb.Append("?");
var j = 0;
var keys = collection.Keys;
foreach (string key in keys)
{
var i = 0;
var values = collection.GetValues(key);
foreach (var value in values)
{
sb.Append(key)
.Append("=")
.Append(value);
if (++i < values.Length)
sb.Append("&");
}
if (++j < keys.Count)
sb.Append("&");
}
return sb.ToString();
}