如何修改查询字符串?
我像这样捕获了查询字符串
qs = Request.QueryString["flag"].ToString();,然后使用修改后的值和response.redirect(url & qs)重新构建查询字符串
发布于 2011-03-16 05:13:23
要根据请求的属性组合所需的目标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);
}发布于 2011-03-16 05:22:09
虽然我不确定我会建议自由使用这种方法,但如果您想通过一些更改来重建路径和查询字符串...您可以将查询字符串转换为可编辑的集合,对其进行修改,然后从新集合重新生成它。
愚蠢的例子。
// 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");发布于 2011-03-16 05:02:41
我不确定我是否理解了你的问题。您只需更改字符串qs并使用。
qs = qs + "modification"
Response.Redirect("this.aspx?flag=" + qs )https://stackoverflow.com/questions/5317947
复制相似问题