C#-NameValueCollection快速转QueryString

How to convert NameValueCollection to a (Query) String

/// <summary>
/// Constructs a QueryString (string).
/// Consider this method to be the opposite of "System.Web.HttpUtility.ParseQueryString"
/// </summary>
public 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());
}

https://leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/

转为Json

Headers = JsonConvert.SerializeObject(Request.Headers)

快速

string.Join(",",Request.Headers.AllKeys.SelectMany(Request.Headers.GetValues, (k, v) => new { key = k, value = v }));

var headersString = string.Join(
    Environment.NewLine,
    Request.Headers.AllKeys.SelectMany(
        Request.Headers.GetValues,
        (k, v) => k + ": " + v));

快速按行输出

string headersString = string.Join("\r\n", context.Request.Headers.Select(s => s.Key + ":" + s.Value.ToString()));

使用换行符号作为连接