【问题标题】:ASP.NET MVC 2.0 Session State Connection Error HandlingASP.NET MVC 2.0 会话状态连接错误处理
【发布时间】:2011-05-24 19:41:54
【问题描述】:
我有一个使用 SQL Server 作为会话状态管理器的 MVC 2.0 站点。我正在尝试解决一个网络错误,在该错误中我的站点失去了与 SQL 服务器本身的连接。
出于非常基本的测试目的,我使用 Global.asax 文件中的 Application_Error(object sender, EventArgs e) 来记录错误,然后将用户转发到一般错误页面。当会话状态管理器尝试再次连接并在无法导致再次调用 Application_Error() 函数时引发异常时,会出现问题。这会导致无限循环。
有没有办法为控制器中的给定操作禁用任何连接到会话状态 SQL 服务器的尝试?
【问题讨论】:
标签:
asp.net-mvc-2
error-handling
session-state-server
【解决方案1】:
对于任何展望未来的人,我通过在 global.asax 文件中使用此代码解决了这个问题:
protected void Application_Error(object sender, EventArgs e) {
// Get Exception
var ex = Server.GetLastError();
// Looking for SqlTimeout, which is inner exception to HttpExecption
// when it occurs.
if (!typeof(HttpException).IsInstanceOfType(ex))
return;
// Clear response.
HttpContext.Current.Response.Clear();
// Check inner
if (ex.InnerException != null && typeof(SqlException).IsInstanceOfType(ex.InnerException)) {
// Clear error
Server.ClearError();
// Redirect to basic html error.
Server.Transfer("/site_down.html", false);
}
else {
// Send to general error page.
Response.Redirect("~/error/", true);
}
}