2012-08-13 133 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