【问题标题】:Custom Error Pages with ASP.NET MVC 5 and Elmah使用 ASP.NET MVC 5 和 Elmah 的自定义错误页面
【发布时间】:2014-05-10 23:45:10
【问题描述】:

我正在尝试在 asp mvc 5 中制作自定义错误页面,但出于某种奇怪的原因,现在要测试我的页面,从 elmah 我正在记录两个错误(我正在测试的真正错误和与错误页面相关的错误不是发现:

未找到视图“错误”或其主视图,或者没有视图引擎支持搜索到的位置。搜索了以下位置: ~/Views/HotTowel/Error.aspx ~/Views/HotTowel/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/HotTowel/Error.cshtml ~/Views/HotTowel/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml

我正在查看这个 url http://doingthedishes.com/2011/09/10/custom-errors-mvc-3-elmah.html,作者遇到了同样的问题,但使用的是 asp.net mvc 3。阅读后,我尝试删除对 HandleErrorAttribute 的调用:

  public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        //filters.Add(new HandleErrorAttribute());
    }
}

但问题仍然存在:我可以看到我的自定义页面,但 asp.net mvc 抛出了两个异常。 有什么帮助吗?

解决方案是重写从 HandleErrorAttribute 派生的类? 喜欢这篇文章:keep getting The view "Error" not found when using Elmah and asp.net mvc 4?

【问题讨论】:

    标签: asp.net-mvc-5 elmah


    【解决方案1】:

    您可以通过ELMAH.MVC 2.0.2 is out 执行以下操作:

    1. disableHandleErrorFilter 设置为true

      <add key="elmah.mvc.disableHandleErrorFilter" value="true" />
      
    2. FilterConfig 类中删除filters.Add(new HandleErrorAttribute());

      public class FilterConfig
      {
          public static void RegisterGlobalFilters(GlobalFilterCollection filters)
          {
            // filters.Add(new HandleErrorAttribute()); // <-- comment out
          }
      }
      

    【讨论】:

      【解决方案2】:

      这里有一个可能的解决方案。我通常在基本控制器类中覆盖OnException 方法。 filterContext.HttpContext.IsCustomErrorEnabled 检查 web.config 中的 &lt;customErrors&gt;showVerboseErrors 变量源自 web.config 中的设置。

      protected override void OnException(ExceptionContext filterContext)
      {
          if (filterContext.HttpContext.IsCustomErrorEnabled)
          {
              //trigger elmah
              Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
      
              //get the last elmah error
              var errorList = new List<ErrorLogEntry>();
              Elmah.ErrorLog.GetDefault(filterContext.HttpContext.ApplicationInstance.Context).GetErrors(0, 1, errorList);
              var error = errorList.LastOrDefault();
      
              //return the custom error page
              filterContext.Result = new ViewResult
              {
                  ViewName = "~/Views/Shared/Error.cshtml",
                  ViewData = new ViewDataDictionary() {
                      { "ErrorDetails", showVerboseErrors && error != null ? filterContext.Exception.Message : null },
                      { "ErrorId", error != null ? error.Id : null }
                  }
              };
      
              //stop further error processing
              filterContext.ExceptionHandled = true;
          }
          else
          {
              base.OnException(filterContext);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2014-06-27
        • 2015-11-14
        • 2010-11-21
        • 2010-10-20
        • 2014-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多