2012-08-13 130 views
2

我在我的應用程序中的異常處理非常相似,此解決方案: http://www.devcurry.com/2012/06/aspnet-mvc-handling-exceptions-and-404.html刷新頁面3

有一個在我的應用程序一個討厭的錯誤,這錯誤是可能的SQL死鎖與另一個過程。這種情況很少發生(每天有1-2次請求因此而失敗),但仍然發生。

如何自動刷新頁面上的SQL死鎖(並在獲取請求時從最終用戶以這種方式隱藏錯誤)?

我可以在Application_Error函數中做到嗎?或者在HandleErrorAttribute中重寫的OnException?

編輯:

我嘲笑了在BaseController一些代碼,我創建了:

protected override void OnException(ExceptionContext filterContext) 
{ 
    Exception ex = filterContext.Exception; 
    SqlException sex = ex as SqlException; 

    if (sex != null && sex.Number == 1205) 
    { 
     Log.Error("Transaction deadlocked with the following exception:"); 
     Log.Exception(sex); 

     //I need to write the logic that refreshes the page here. 
    } 
    else 
    { 
     Log.Error("Application error with the following exception:"); 
     Log.Exception(ex); 
    } 

    base.OnException(filterContext); 
} 

我需要刷新部件的幫助。

回答

0

我會通過重寫控制器的OnException()方法來處理它。如果您從自定義基礎控制器繼承了所有控制器,那麼最好的做法是進行覆蓋,以保持解決方案的一致性和乾燥性。

+0

所以寫這刷新頁面中onException的()函數是乾的邏輯。你能幫我寫出實際的邏輯嗎? – SoonDead 2012-08-13 14:18:52

+0

這高度依賴於您的應用程序。但是一種普遍的模式是在視圖的視圖模型中包含視圖的路徑,並將OnException重定向到該URL。這應該刷新視圖。 – JTMon 2012-08-13 14:27:54

+0

問題是當前的URL與我想要重定向的位置相同。但是Response.Redirect(Request.RawUrl)工作正常。 – SoonDead 2012-08-13 14:36:51

0

只需添加婁代碼,之前base.OnException(filterContext);

// Stop any other exception handlers from running 
filterContext.ExceptionHandled = true; 
+0

這會做什麼? – SoonDead 2015-01-19 14:33:41