Pardeep Kumar Pardeep Kumar - 3 months ago 26
jQuery Question

How can we read or get session timeout with jquery from web.config so that we can handle session expiry in mvc

How to get Session Value from webconfig through ajax call

I have tried the following code

<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>

Answer

Actually you are doing this in MVC; you might need to use "@" syntax for that. Please try this, it may be of help.

 <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>
Comments