2016-09-22 53 views
0

當我的會話將要在.net Mvc中清除時,我必須顯示一個彈出窗口。檢測會話超時並顯示當會話即將在mvc中清除時彈出

例如:enter link description here

我想同樣的事情在Mvc..what實現我應該怎麼辦???

這裏我有嘗試一些代碼:

這是控制器頁面(錯誤出現的是ClientScript不列入存在):

Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config"); 
     SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState"); 
     int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60; 
     ClientScript.RegisterClientScriptBlock(this.GetType(),"SessionAlert", "SessionExpireAlert(" + timeout + ");", true); 

這裏是我的看法腳本(我是從控制器調用):

<script type="text/javascript"> 
function SessionExpireAlert(timeout) { 
    var seconds = timeout/1000; 
    document.getElementsByName("secondsIdle").innerHTML = seconds; 
    document.getElementsByName("seconds").innerHTML = seconds; 
    setInterval(function() { 
     seconds--; 
     document.getElementById("seconds").innerHTML = seconds; 
     document.getElementById("secondsIdle").innerHTML = seconds; 
    }, 1000); 
    setTimeout(function() { 
     //Show Popup before 20 seconds of timeout. 
     $find("mpeTimeout").show(); 
    }, timeout - 20 * 1000); 
    setTimeout(function() { 
     window.location = "Expired.aspx"; 
    }, timeout); 
}; 
function ResetSession() { 
    //Redirect to refresh Session. 
    window.location = window.location.href; 
} 
</script> 
+0

恐怕你不能這樣做。頁面對象可以在Web窗體中調用,因爲服務器端代碼是緊密耦合的,但是你不能使用mvc –

+0

來做到這一點爲什麼???如何在mvc中執行..i認爲必須有任何方式來做到這一點..如果您有任何解決方案告訴我.. –

+0

您可以使用JQuery JavaScript庫並觸發ajax請求,並在成功處理程序中執行您所需的操作。 –

回答

0

據我所知,通過一個腳本內容進入視野時,MVC控制器不需要ClientScript.RegisterClientScriptBlock。可以只註冊使用ViewBag/ViewDataHtml.Raw像這樣的腳本塊,只需確保控制器返回查看與ViewBag/ViewData包括:

public ActionResult WarnTimeout() 
{ 
    Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.Config"); 
    SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState"); 
    int timeout = (int)section.Timeout.TotalMinutes * 1000 * 60; 
    ViewBag.Script = "\r\n<script type='text/javascript'>\r\nSessionExpireAlert(" + timeout + ");\r\n</script>\r\n"); 

    return View(); 
} 

// View (use Html.Raw to include script tag into view) 
@Html.Raw(ViewBag.Script) 

<script type="text/javascript"> 
function SessionExpireAlert(timeout) { 
    var seconds = timeout/1000; 
    document.getElementsByName("secondsIdle").innerHTML = seconds; 
    document.getElementsByName("seconds").innerHTML = seconds; 
    setInterval(function() { 
     seconds--; 
     document.getElementById("seconds").innerHTML = seconds; 
     document.getElementById("secondsIdle").innerHTML = seconds; 
    }, 1000); 
    setTimeout(function() { 
     //Show Popup before 20 seconds of timeout. 
     $find("mpeTimeout").show(); 
    }, timeout - 20 * 1000); 
    setTimeout(function() { 
     // when using MVC, you may use Url.Action instead of providing ASPX relative path 
     window.location = "@Url.Action("Expired", "Controller")"; 
    }, timeout); 
}; 
function ResetSession() { 
    //Redirect to refresh Session. 
    window.location = window.location.href; 
} 
</script> 

或者,jQuery的AJAX調用($.ajax({ ... }))與某些數據隨後被提供可以使用在success部分內設置期望的警報框操作。

歡迎任何建議。

參考類似的問題:

How do I warn the user that their web session is about to time out?

+0

我想Html.Raw只會將你的腳本註冊到視圖中,但它不會觸發腳本。要做到這一點,你必須顯式觸發頁面加載,如果用戶正在使用get postback.Your替代解決方案是更好處理 –

2

獲取用戶會話時的第一超時:

int sessionTimeout = HttpContext.Current.Session.Timeout; 
DateTime timeoutDate = DateTime.Now.AddMinutes(sessionTimeout); 

可以傳遞到您的視圖通過模型或ViewBag和使用JavaScript定時器彈出警報。 (由於每次請求刷新會話,這將是一個準確的超時)。

查看:

@{ 
    int milliseconds = (int)ViewBag.sessionTimeout * 60 * 1000; 
} 
setTimeout(function(){alert("SessionPop!");}, @milliseconds.ToString()); 

但由於會議是在打控制器動作刷新你可能想提供實時支持(特別是如果你的應用程序會在多個選項卡中使用或大量使用AJAX調用)。當新會話超時將到期時,您可以使用SignalR實時更新用戶。