csharp 将NameValueCollection转换为Querystring
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 将NameValueCollection转换为Querystring相关的知识,希望对你有一定的参考价值。
/// <summary>
/// Constructs a NameValueCollection into a query string.
/// </summary>
/// <remarks>Consider this method to be the opposite of "System.Web.HttpUtility.ParseQueryString"</remarks>
/// <param name="parameters">The NameValueCollection</param>
/// <param name="delimiter">The String to delimit the key/value pairs</param>
/// <returns>A key/value structured query string, delimited by the specified String</returns>
public static string ToQueryString(this NameValueCollection parameters, String delimiter, Boolean omitEmpty)
{
if (String.IsNullOrEmpty(delimiter))
delimiter = "&";
Char equals = '=';
List<String> items = new List<String>();
for (int i = 0; i < parameters.Count; i++)
{
foreach (String value in parameters.GetValues(i))
{
Boolean addValue = (omitEmpty) ? !String.IsNullOrEmpty(value) : true;
if (addValue)
items.Add(String.Concat(parameters.GetKey(i), equals, HttpUtility.UrlEncode(value)));
}
}
return String.Join(delimiter, items.ToArray());
}
//much easier:
public static string ToQueryString(this NameValueCollection parameters)
{
var items = parameters.AllKeys
.SelectMany(parameters.GetValues, (k, v) => k + "=" + HttpUtility.UrlEncode(v))
.ToArray();
return String.Join("&", items);
}
HttpContext context = HttpContext.Current;
string formValues = "";
for (int i = 0; i < context.Request.Form.Count; i++)
{
string strValues = "";
foreach (var strValue in context.Request.Form.GetValues(context.Request.Form.Keys[i]))
{
strValues += strValue + ",";
}
formValues += context.Request.Form.Keys[i] + " - " + strValues + Environment.NewLine;
}
以上是关于csharp 将NameValueCollection转换为Querystring的主要内容,如果未能解决你的问题,请参考以下文章
csharp 将数据导出为excel
csharp 将页面加载到内容控件
csharp 将脚本添加到pdf
csharp 将RDLC渲染为PDF输出
csharp 将RDLC渲染为PDF输出
csharp 将用户添加到调查中