Error pages not found, throwing ELMAH error with Custom Error Pages

前端 未结 2 1465
广开言路
广开言路 2020-12-29 10:14

I\'ve made some modifications to Global.asax so that I can show custom error pages (403, 404, and 500) Here\'s the code:

    public class MvcApplication : Sy         


        
相关标签:
2条回答
  • 2020-12-29 11:07

    If you are running in IIS 7 integrated mode, you will need to add Response.TrySkipIisCustomErrors = true; in Application_Error. Otherwise IIS will still redirect the client to a custom error page, despite anything you do in code.

    See here for additional details: http://www.west-wind.com/weblog/posts/2009/Apr/29/IIS-7-Error-Pages-taking-over-500-Errors

    Edit: here's the body of my 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));
    }
    
    0 讨论(0)
  • 2020-12-29 11:14

    Finally got it working to my satisfaction...

    The Elmah.Mvc package applies a "hidden" error handler. I've disabled this by adding the following line in web.config <appSettings> (the value was set to "false" by default from nuget install)

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

    So, now my errors propagate up to Application_Error and are logged by Elmah, bypassing the Elmah filter, and display the proper error page (not the one in /shared/error.cshtml)

    0 讨论(0)
提交回复
热议问题