会话超时异常 [英] Session timeout exception
问题描述
如果20分钟网站空闲,稍后如果点击导航按钮
则会引发用户声明对象引用未设置为对象实例的异常。怎么避免这个?
谢谢。
默认超时是20分钟
< a href =http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.timeout%28v=vs.110%29.aspx> http://msdn.microsoft.com /en-us/library/system.web.sessionstate.httpsessionstate.timeout%28v=vs.110%29.aspx [ ^ ]
您的代码必须检查如果超时,将用户重定向到某个默认页面,请检查:
redirect-to-login-page-after-session-timeout [ ^ ]
作为最佳实践你笑首先存在检查会话
if (会话[ myVal]!= null )
{
string myVal = Session [ myVal]。的ToString();
}
else
{
Response.Redirect( Login.aspx)
}
如果直接拨打Session [myVal]。ToString()
,您将得到'对象引用未设置为对象的实例'。如果会话值为null。
您可能需要在每个页面上执行此操作。但是通过使用全局aspx文件有一个简单的方法
检查这些答案 [ ^ ]
如果您需要增加会话超时,请检查这些答案 [ ^ ]
If for 20 minutes the website is idle and later if a navigation button is clicked
an exception is thrown for the user stating that 'Object reference not set to an instance of an object'. How to avoid this?
Thanks.
The Default Timeout Is 20 minutes
http://msdn.microsoft.com/en-us/library/system.web.sessionstate.httpsessionstate.timeout%28v=vs.110%29.aspx[^]
Your code have to check that if timeout, redirect the user to some default page, check this out:
redirect-to-login-page-after-session-timeout[^]
As a best practice you should check session exist first
if(Session["myVal"] != null) { string myVal = Session["myVal"].ToString(); } else { Response.Redirect("Login.aspx") }
if you directly callSession["myVal"].ToString()
, you will get "'Object reference not set to an instance of an object'." in case of session value null.
you may need to do this on each and every page. but there is a easy way by using global aspx file
Check these answers[^]
if you need to increase session timeout, then check these answers[^]
这篇关于会话超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!