2010-12-13 74 views
0

我有一個使用SQL Server作爲會話狀態管理器的MVC 2.0網站。我正試圖解決一個網絡錯誤,其中我的網站失去了與SQL服務器本身的連接。ASP.NET MVC 2.0會話狀態連接錯誤處理

對於非常基本的測試目的,我在Global.asax文件中使用Application_Error(object sender,EventArgs e)記錄錯誤,然後將用戶轉發到常規錯誤頁面。當轉發到一般錯誤頁面時,會話狀態管理器嘗試再次連接並在它不能再次調用Application_Error()函數時拋出異常。這導致無限循環。

是否有一種方法可以禁用任何連接到控制器中給定操作的會話狀態SQL服務器的嘗試?

回答

0

對於任何人都希望在未來我通過在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); 
    } 
}