如何避免由于超时而出现 HttpException
我正在开发一个由 .NET asp/C# 代码支持的网站。客户端要求会话有 25 分钟的超时时间。然而,有时该网站会被使用,并且用户会长时间保持连接(超过 25 分钟)。 Session_End 被触发:
protected void Session_End(Object sender, EventArgs e)
{
Hashtable trackingInformaiton = (Hashtable)Application["trackingInformation"];
trackingInformaiton.Remove(Session["trackingID"]);
}
用户在一段时间后返回,但当他们与网站交互时,他们收到错误,我们收到此电子邮件通知:
用户:未经身份验证的用户
错误:System.Web.HttpException
描述:加载视图状态失败。正在加载视图状态的控制树必须与在上一个请求期间用于保存视图状态的控制树相匹配...
堆栈跟踪的重要部分是System.Web.UI.Control.AddedControl
。显然,服务器已经丢弃了会话数据,并正在发送新数据,但客户端正在尝试处理旧数据。因此,错误是“正在加载视图状态的控制树[不]与先前请求期间用于保存视图状态的控制树匹配。”
所以这是问题。当连接超时时,如何强制指示用户的浏览器重定向到“您已注销”屏幕? (我应该将其添加到 Session_End 方法中吗?)
I'm working on a website powered by .NET asp/C# code. The clients require that sessions have a 25 minute timeout. However, sometimes the site is used, and a user stays connected for long periods of time (longer than 25 mins). Session_End is triggered:
protected void Session_End(Object sender, EventArgs e)
{
Hashtable trackingInformaiton = (Hashtable)Application["trackingInformation"];
trackingInformaiton.Remove(Session["trackingID"]);
}
The user returns some time later, but when they interact with the website, they get an error, and we get this email notification:
User: Unauthenticated User
Error: System.Web.HttpException
Description: Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request...
The telling part of the stack trace is System.Web.UI.Control.AddedControl
. Apparently, the server has thrown away the session data, and is sending new data, but the client is trying to deal with old data. Hence the error that "the control tree into which viewstate is being loaded [doesn't] match the control tree that was used to save the viewstate during the prevoius request."
So here's the question. How can I force instruct the user's browser to redirect to a "you're logged out" screen when the connection times out? (Is it something I should add to the Session_End method?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您需要使用一些客户端脚本来执行此操作。尽管 Session_End 会触发,但如果用户没有首先执行某些操作(即发送请求),Web 服务器实际上无法向浏览器传达会话超时消息。因此,您必须让一些定时的客户端脚本启动该消息。
I think you'll need to use some client-side script to do this. Despite the fact that Session_End fires, the web server can't actually communicate a Session timeout message to the browser without the user first performing some action (i.e. sending a request.) So you'll have to have some timed client side script initiate that.
更简单的是使用 Html 元标记“刷新”。以下行将在页面加载 5 分钟后触发:
也许这就是您所需要的。
Even simpler is to use the Html meta tag "refresh". The following line will fire 5 minutes after the page was loaded:
Maybe this is what you need.
类似于 javascript 的 setTimeout 函数。
例如
在页面onload函数
setTimeout("redirect()", 1500000); //25 分钟超时
如果您的页面上有任何 AJAX,您应该清除超时并在新内容加载时重新开始。
something like javascript's setTimeout function.
e.g.
in page onload function
setTimeout("redirect()", 1500000); //25 minute timeout
if you have any AJAX on your page, you should clear the timeout and start it over again when your new content loads.