2012-02-22 58 views
1

我正在使用asp.net开发网站。在我的网站登录后,我为记录的用户设置了会话,以供我的网站需要。并在每一页上我检查该会话是否为空或有价值,并根据我重定向到登录页面的值。会话过期后重定向到先前浏览的网页

我的问题是,登录后,我想重定向到之前浏览的网页,我通过使用特定的页面上以下way-

代码做了检查会话值 -

if (Session["EmployeeID"] != null) 
    { 

    } 
    else 
    { 
     Response.Redirect(ResolveUrl("~/login.aspx?IsTimeout=1"), true); 
    } 
在登录

代码PAGE-

If Request.QueryString("IsTimeout") IsNot Nothing Then 
    If Request.QueryString("IsTimeout") = "1" Then 
     Login1.DestinationPageUrl = Request.UrlReferrer.ToString() 
    End If 
End If 

是否有❖以正确的方式做到这一点?

回答

3

我不知道正确的方式...但即时做这样的事情..

我有一个请求登录页面,并传递引用的参数的功能

protected void RequestLogin() 
    { 
     string OriginalUrl = HttpContext.Current.Request.RawUrl; 
     string LoginPageUrl = "~/LogIn.aspx"; 
     HttpContext.Current.Response.Redirect(String.Format("{0}?ReturnUrl={1}", 
     LoginPageUrl, OriginalUrl)); 
    } 

,这里是重定向登录后。

if (this.Request.QueryString["ReturnUrl"] !=null) 
    { 
     this.Response.Redirect(Request.QueryString["ReturnUrl"].ToString()); 
    } 
    else 
    { 
     this.Response.Redirect("Default.aspx"); 
    } 
+0

当有此不予办理查询字符串变量的数量,在这种情况下使用会话来存储returnurl。 Session [“ReturnUrl”] = HttpContext.Current.Request.RawUrl; – Roshe 2012-08-17 03:05:07

+0

即使查询字符串变量也可以工作。 会话有时很棘手,因为它不是线程安全的。 – 2012-08-21 12:03:21

0

可以完成这通过使用Request.UrlReferrer.ToString()。这会给你用户所在的最后一页的名称。那么你可以做一个response.redirect(Request.UrlReferrer.ToString())或类似的东西。对不起,伪代码。

0

保存前一页面的网址,查看状态时,从另一个页面重定向页面,当回传生成然后重定向到最后一页..

试试这个:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if(!IsPostBack) 
    { 
     ViewState["RefUrl"] = Request.UrlReferrer.ToString(); 
    } 
    if (Session["EmployeeID"] != null) 
    { 

    } 
    else 
    { 
     object refUrl = ViewState["RefUrl"]; 
     if (refUrl != null) 
      Response.Redirect((string)refUrl); 
    } 
} 

编号: How to go back to the previous page in ASP.NET 2.0
天冬氨酸.net论坛 - redirect to previous page RSS

0

是否有任何其他方式以正确的方式做到这一点?

那么我会争辩说有。当ASP.NET提供.NET Forms身份验证时,不要重写身份验证,超时和重定向。如果会话超时,它会自动添加'ReturnUrl',然后您可以在您的身份验证代码中处理。检查出this link的源代码和实施表单身份验证,并利用其他功能的讨论...

0

在我的愚见,我认为该问题的最佳解决方案是在您的项目集中页面工作..根据这一点,我们不会找到一个合适的页面超过我们的项目的母版页做工作。我会写一些代码来展示我们如何能够实现这个问题:

// In Login Page .. 
// I used a submit button to figure out the event of my code .. 
protected void ButtonInsert_Click(object sender, EventArgs e) 
{ 
    Session.Add("EmployeeID", 100); // This 100 is just a fixed value .. 

    if (!String.IsNullOrEmpty(Request.QueryString["Red_Page"])) 
    { 
     Response.Redirect(Request.QueryString["Red_Page"]); 
    } 
    else 
    { 
     Response.Redirect("EmployeeDepartment.aspx"); 
    } 
} 

// In Master Page .. 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     // In Master Page 
     if (Session["EmployeeID"] != null) 
     { 
      // Write ur business Code 
     } 
     else 
     { 
      // I stored the Page URL in uery String. 
      // Stroing the URL in a cookie will be also a good alternative solution .. 
      string CurrentURLBeofreTimeout = Request.RawUrl; 
      string LoginURL = "login.aspx"; 
      string newURL = string.Format("{0}?Red_Page={1}", LoginURL, CurrentURLBeofreTimeout); 
      Response.Redirect(newURL); 


     } 
    } 

} 
相关问题