我们如何从web.config中使用jquery读取或获取会话超时,以便我们可以在mvc中处理会话到期

时间:2014-09-24 04:29:58

标签: jquery asp.net-mvc c#-4.0

如何通过ajax调用从web配置获取会话值我试过以下代码

<script language="javascript" type="text/javascript">
       var sessionTimeoutWarning = 
    "<%= System.Configuration.ConfigurationSettings.AppSettings
    ["SessionWarning"].ToString()%>";
        var sessionTimeout = "<%= Session.Timeout %>";

        var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
        setTimeout('SessionWarning()', sTimeout);

        function SessionWarning() {
var message = "Your session will expire in another " + 
    (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + 
    " mins! Please Save the data before the session expires";
alert(message);
        }
</script>

2 个答案:

答案 0 :(得分:5)

其实你是在MVC中这样做的;您可能需要使用"@"语法。请试试这个,它可能会有所帮助。

 <script language="javascript" type="text/javascript">
     var sessionTimeoutWarning =    @System.Configuration.ConfigurationSettings.AppSettings["SessionWarning"].ToString();
     var sessionTimeout = @Session.Timeout;    
     var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
        setTimeout('SessionWarning()', sTimeout);

   function SessionWarning() {
     var message = "Your session will expire in another " + (parseInt(sessionTimeout) - parseInt(sessionTimeoutWarning)) + 
                   " mins! Please Save the data before the session expires";
     alert(message);
   }
</script>

答案 1 :(得分:0)

步骤1:在Controller中创建ActionMethod / Function

public string IsUserLoggedIn()
        {
            if (Session["UserId"] != null) // check session here
                return "true";
            else
                return "false";
        }

第2步:创建ajax调用。如果返回true,则用户登录,否则不登录。

function IsUserLoggedIn() {
        var lsValue = true;
        $.ajax({
            type: "Post",
            url: "IsUserLoggedIn",
            datatype: "json",
            async: false,
            success: function (foData) {
                if (foData == "true")
                    lsValue = true;
                else
                    lsValue = false;
            },
            error: function (xhr, ajaxOptions, thrownError) {
            },
            complete: function (e) {
            }
        });
        return lsValue;
    }