Url跳过HttpContext.Current.Request.Url.AbsoluteUri中的第二个查询字符串参数

时间:2013-06-25 09:50:16

标签: c# asp.net url absolute-path querystringparameter

我有一个类,我继承所有页面以检查会话超时然后重定向到登录页面这里是代码

public class WMUserBase:System.Web.UI.Page
{
    public WMUserBase()
    {

    }
    protected override void OnLoad(System.EventArgs e)
    {
        CheckSecurity();
        base.OnLoad(e);
    }
    public virtual void CheckSecurity()
    {
        if (Session["WMuserId"] == null || Session["WMuserId"].ToString() == "")
        {
            Response.Redirect("Login.aspx?ReturnUrl=" + HttpContext.Current.Request.Url.AbsoluteUri);
        }
    }
}

每个页面都继承此类,如果会话超时,页面现在重定向到登录页面,我使用了Response.Redirect("Login.aspx?ReturnUrl=" + HttpContext.Current.Request.Url.AbsoluteUri);  但如果我的查询字符串有两个参数,例如http://localhost/mala3ibnav2/WeeklyManager/TeamSetup.aspx?Gid=MTQ=&Mid=Mg==  在AbsoluteUril & Mid = Mg == 有时会被忽略 以下是登录页面登录按钮单击事件的代码

 if(string.IsNullOrEmpty(ReturnUrl))
                {
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    Response.Redirect(ReturnUrl);
                }

现在为什么在查询字符串中跳过第二个参数,我应该使用什么来代替HttpContext.Current.Request.Url.AbsoluteUri

1 个答案:

答案 0 :(得分:2)

您必须对网址进行编码:

Response.Redirect("Login.aspx?ReturnUrl=" + HttpUtility.UrlEncode(HttpContext.Current.Request.Url.AbsoluteUri));

如果您使用表单身份验证,则可以将身份验证超时设置为比会话超时少一分钟。然后你不必自己编写代码,因为ASP.NET会在超时到期后自动将用户重定向到登录页面。