【问题标题】:Refresh page on deadlock in ASP.NET MVC 3刷新 ASP.NET MVC 3 中的死锁页面
【发布时间】:2012-08-13 12:55:02
【问题描述】:

我的应用程序中有一个与此解决方案非常相似的异常处理: http://www.devcurry.com/2012/06/aspnet-mvc-handling-exceptions-and-404.html

我的应用程序中存在一个令人讨厌的错误,其中 sql 可能与其他进程死锁。这种情况很少发生(每天有 1-2 个请求因此失败),但它仍然会发生。

如何在 sql deadlock 上自动刷新页面(并在获取请求时以这种方式向最终用户隐藏错误)?

我可以在 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);
}

我需要刷新部分的帮助。

【问题讨论】:

    标签: asp.net-mvc-3 refresh deadlock


    【解决方案1】:

    我会通过重写控制器的 OnException() 方法来处理它。最好从自定义基础控制器继承所有控制器,在该基础控制器中进行覆盖以保持解决方案的一致性和干燥性。

    【讨论】:

    • 所以在 OnException() 函数中编写刷新页面的逻辑是 DRY。你能帮我写出实际的逻辑吗?
    • 这高度依赖于您的应用程序。但是一种通用模式是在视图模型中包含视图的路径,并根据该视图键入视图并将 OnException 重定向到该 url。这应该会刷新视图。
    • 问题是当前 URL 与我要重定向的位置相同。但是 Response.Redirect(Request.RawUrl) 工作正常。
    【解决方案2】:

    只需在base.OnException(filterContext);之前添加以下代码

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

    【讨论】:

      猜你喜欢
      • 2012-01-04
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多