【问题标题】:Error pages not found, throwing ELMAH error with Custom Error Pages未找到错误页面,使用自定义错误页面引发 ELMAH 错误
【发布时间】:2013-05-18 00:19:01
【问题描述】:

我对 Global.asax 进行了一些修改,以便可以显示自定义错误页面(403、404 和 500)这是代码:

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            if (Context.IsCustomErrorEnabled)
            {
                ShowCustomErrorPage(Server.GetLastError());
            }
        }

        private void ShowCustomErrorPage(Exception exception)
        {
            HttpException httpException = exception as HttpException;
            if (httpException == null)
            {
                httpException = new HttpException(500, "Internal Server Error", exception);
            }

            Response.Clear();
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("fromAppErrorEvent", true);

            switch (httpException.GetHttpCode())
            {
                case 403:
                    routeData.Values.Add("action", "AccessDenied");
                    break;

                case 404:
                    routeData.Values.Add("action", "NotFound");
                    break;

                case 500:
                    routeData.Values.Add("action", "ServerError");
                    break;

                default:
                    routeData.Values.Add("action", "DefaultError");
                    routeData.Values.Add("httpStatusCode", httpException.GetHttpCode());
                    break;
            }

            Server.ClearError();

            IController controller = new ErrorController();
            controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
        }
    }

我还在我的 Web.Config 中添加了以下内容:

<customErrors mode="On">
    <!-- There is custom handling of errors in Global.asax -->
</customErrors>

自定义错误页面正确显示,ELMAH 将正确记录(故意)抛出的错误。但是 ELMAH 也会捕获并记录一个额外的错误:

System.InvalidOperationException: The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/account/Error.aspx ~/Views/account/Error.ascx ~/Views/Shared/Error.aspx ~/Views/Shared/Error.ascx ~/Views/account/Error.cshtml ~/Views/account/Error.vbhtml ~/Views/Shared/Error.cshtml ~/Views/Shared/Error.vbhtml

我的第一直觉让我在过滤器配置中禁用了全局HandleErrorAttribute。而且,类似的 SO 问题,例如:MVC problem with custom error pages 让我相信我的怀疑是正确的。但即使在禁用全局HandleErrorAttribute 之后,我仍然收到无法找到错误视图的错误!是什么赋予了?我唯一的另一个预感是我的基本控制器源自System.Web.Mvc.Controller 我试图检查源代码以查看HandleErrorAttribute 是否应用于System.Web.Mvc.Controller 但无法收集任何东西...

更新: 我尝试覆盖我的基本控制器以将异常标记为这样处理:

protected override void OnException(ExceptionContext filterContext)
{
    filterContext.ExceptionHandled = true;
    base.OnException(filterContext);
}

但这并没有解决问题。

更新2: 我在共享视图中放置了一个 Error.aspx 文件,只是为了看看会发生什么。当它在那里时,ELMAH 记录强制异常,然后提供共享视图 - 它永远不会到达 Application_Error() .... 不太清楚该怎么做。

【问题讨论】:

    标签: c# asp.net-mvc-4 elmah


    【解决方案1】:

    如果您在 IIS 7 集成模式下运行,则需要在 Application_Error 中添加 Response.TrySkipIisCustomErrors = true;。否则 IIS 仍会将客户端重定向到自定义错误页面,尽管您在代码中执行了任何操作。

    更多详情请看这里:http://www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors

    编辑:这是我的 Application_Error 的主体:

    if (HttpContext.Current != null)
    {
        Server.ClearError();
        Response.TrySkipIisCustomErrors = true;
        RouteData data = new RouteData();
        data.Values.Add("controller", "Error");
        data.Values.Add("action", "Error");
        IController controller = new MyApp.Controllers.ErrorController();
        controller.Execute(new RequestContext(new HttpContextWrapper(Context), data));
    }
    

    【讨论】:

    • 我尝试在ShowCustomErrorPage 中的Response.Clear(); 之前和之后添加它。都没有工作:(
    • 尝试在Response.ClearError()之后设置。这就是我拥有的地方。
    【解决方案2】:

    终于让我满意了...

    Elmah.Mvc 包应用“隐藏”错误处理程序。我已通过在 web.config &lt;appSettings&gt; 中添加以下行来禁用此功能(默认情况下,从 nuget install 将值设置为“false”)

    <add key="elmah.mvc.disableHandleErrorFilter" value="true" />
    

    所以,现在我的错误传播到 Application_Error 并由 Elmah 记录,绕过 Elmah 过滤器,并显示正确的错误页面(不是 /shared/error.cshtml 中的那个)

    【讨论】:

    • 另外,请确保从 FilterConfig 中删除默认的 MVC 错误处理程序
    • beletsky.net/2012/11/elmahmvc-202-is-out.html "它是 elmah.mvc.disableHandleErrorFilter。默认情况下它是 false,这意味着使用来自 ELMAH.MVC 的 HandleErrorAttribute()。要禁用它,只需将此设置设置为 true。"
    • 搜索了这个解决方案的高位和低位!
    • 这是此事件的最终解决方案,请记住始终应用删除过滤器。
    猜你喜欢
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 2011-03-25
    • 2015-08-26
    • 2019-11-28
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    相关资源
    最近更新 更多