2012-02-22 81 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); 


     } 
    } 

} 
相關問題