【问题标题】:how to fix request.querystring before pageload如何在页面加载之前修复 request.querystring
【发布时间】:2009-10-04 12:10:21
【问题描述】:
我之前的问题是urlrewriter problem: Query string is duplicated shown?
我正在开发 asp.net 网站。但是有一个问题。有这样的重复查询字符串www.domainname.com/default.aspx?Query=Value1&Query=Value2
我使用了太多像Request.QueryString["Query"] 这样的页面。但是这个返回 Value1,Value2 。我不想将这个问题修复到太多页面。我想在页面加载之前修复查询字符串。我认为Maybe会在global.asax上写一些函数。但是不知道怎么写。
你有什么想法吗?
【问题讨论】:
标签:
c#
asp.net
query-string
【解决方案1】:
我相信 Request.QueryString 本身是只读的。您可以设置自己的集合,其中包含您想要使用的任何内容:
public Dictionary<string, object> qsValues = new Dictionary<string, object>();
foreach (string key in Request.QueryString.Keys) {
if (Request.QueryString[key].Count > 1) {
qsValues[key] = Request.QueryString[key][0];
}
else {
qsValues[key] = Request.QueryString[key];
}
}
或者只访问代码中该查询字符串参数值列表中的第一个条目:
if (Request.QueryString["Query"].Count > 1) {
queryValue = Request.QueryString[0];
}
【解决方案2】:
public static bool bi = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (bi == false)
{
bi = true;
Response.Redirect("MainPage.aspx?id=null");
}
else if (bi)
{
string s = Request.QueryString["id"].ToString();
if (s != "null")
{
switch (s)
{
case "News":
{
ProjectsManagment.Controls.AllNews n = new Controls.AllNews();
MainContentsAsp.Controls.Add(n);
break;
}
default:
break;
}
}
}
}
}