Asp.net 更改Request.QueryString值

Asp.net 更改Request.QueryString值,asp.net,Asp.net,如何修改查询字符串 我已经像这样捕获了查询字符串 qs = Request.QueryString["flag"].ToString(); 然后用修改后的值重新生成查询字符串 和响应。重定向(url&qs)到它我不确定我是否理解你的问题。您只需更改字符串qs并使用 qs = qs + "modification" Response.Redirect("this.aspx?flag=" + qs ) 我不确定我是否理解你的问题。您只需更改字符串qs并使用 qs = qs + "modi

如何修改查询字符串

我已经像这样捕获了查询字符串

qs = Request.QueryString["flag"].ToString();
然后用修改后的值重新生成查询字符串
和响应。重定向(url&qs)到它

我不确定我是否理解你的问题。您只需更改字符串qs并使用

qs = qs + "modification"    
Response.Redirect("this.aspx?flag=" + qs )

我不确定我是否理解你的问题。您只需更改字符串qs并使用

qs = qs + "modification"    
Response.Redirect("this.aspx?flag=" + qs )

Request
类中的内容处理使您进入页面的请求。您无法编辑它,因为客户端构造了它,而不是服务器。

Request类中的内容处理使您进入页面的请求。您无法编辑它,因为它是由客户端而不是服务器构建的。

要根据请求的属性组合所需的目标URL,请使用以下内容:

string destUrl = string.Format("{0}://{1}{2}/", Request.Url.Scheme, Request.Url.Authority, Request.Url.AbsolutePath);
if (destUrl.EndsWith("/"))
    destUrl = destUrl.TrimEnd(new char[] { '/' });
if (!string.IsNullOrEmpty(Request.QueryString["paramName"])) {
    destUrl = string.Format("{0}?paramName={1}", destUrl, "paramValueHere");
    Response.Redirect(destUrl);
}

要根据请求的属性组合所需的目标URL,请使用以下内容:

string destUrl = string.Format("{0}://{1}{2}/", Request.Url.Scheme, Request.Url.Authority, Request.Url.AbsolutePath);
if (destUrl.EndsWith("/"))
    destUrl = destUrl.TrimEnd(new char[] { '/' });
if (!string.IsNullOrEmpty(Request.QueryString["paramName"])) {
    destUrl = string.Format("{0}?paramName={1}", destUrl, "paramValueHere");
    Response.Redirect(destUrl);
}

虽然我不确定我是否建议自由地使用这种方法,但如果您想通过一些更改来重建路径和查询字符串。。。您可以将查询字符串转换为可编辑集合,对其进行修改,然后从新集合重新生成查询字符串

愚蠢的例子

// create dictionary (editable collection) of querystring
var qs = Request.QueryString.AllKeys
            .ToDictionary(k => k, k => Request.QueryString[k]);

// modify querystring
qs["flag"] = "2";

// rebuild querystring
var redir = string.Format("{0}{1}", Request.Path,
            qs.Aggregate(new StringBuilder(),
                 (sb, arg) => sb.AppendFormat("{0}{1}={2}", 
                 sb.Length > 0 ? "&" : "?", arg.Key, arg.Value)));

// do something with it
Response.Redirect(redir);
虽然我绝对不会推荐以下代码用于生产代码,但出于测试目的,您可以使用反射使querystring集合可编辑

// Get the protected "IsReadOnly" property of the collection
System.Reflection.PropertyInfo prop = Request.QueryString.GetType()
    .GetProperty("IsReadOnly", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

// Set the property false (writable)
prop.SetValue(Request.QueryString, false, null);

// Have your way with it.
Request.QueryString.Add("flag", "2");

虽然我不确定我是否建议自由地使用这种方法,但如果您想通过一些更改来重建路径和查询字符串。。。您可以将查询字符串转换为可编辑集合,对其进行修改,然后从新集合重新生成查询字符串

愚蠢的例子

// create dictionary (editable collection) of querystring
var qs = Request.QueryString.AllKeys
            .ToDictionary(k => k, k => Request.QueryString[k]);

// modify querystring
qs["flag"] = "2";

// rebuild querystring
var redir = string.Format("{0}{1}", Request.Path,
            qs.Aggregate(new StringBuilder(),
                 (sb, arg) => sb.AppendFormat("{0}{1}={2}", 
                 sb.Length > 0 ? "&" : "?", arg.Key, arg.Value)));

// do something with it
Response.Redirect(redir);
虽然我绝对不会推荐以下代码用于生产代码,但出于测试目的,您可以使用反射使querystring集合可编辑

// Get the protected "IsReadOnly" property of the collection
System.Reflection.PropertyInfo prop = Request.QueryString.GetType()
    .GetProperty("IsReadOnly", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

// Set the property false (writable)
prop.SetValue(Request.QueryString, false, null);

// Have your way with it.
Request.QueryString.Add("flag", "2");

嗯。你几乎描述了怎么做。你的问题是什么?嗯。你几乎描述了怎么做。您的问题是什么?客户如何构建NameValueCollection?我想你的意思是服务器根据客户机提供的数据构造了它。客户机如何构造NameValueCollection?我想你的意思是服务器根据客户机提供的数据构建它。