37

I've spent a lot of time trying to figure out a workaround for this to no avail, so I thought I'd see if anyone here has an idea.

I'm using Elmah in my ASP.NET MVC3 application. I'm using the exact same code from the accepted answer in the previous link.

I also have this code in my Global.asax for displaying error pages with the correct HTTP response:

    /// <summary>
    /// The customErrors functionality provided by ASP.NET results in HTTP 302 redirects occurring which doesn't accurately reflect what the real HTTP code of the response was.
    /// This method can be used to handle specific HTTP codes without an intermediate redirect.
    /// </summary>
    protected void Application_Error() {
        var exception = Server.GetLastError();
        var httpException = exception as HttpException;
        Response.Clear();
        Server.ClearError();
        var routeData = new RouteData();
        routeData.Values["controller"] = "Error";
        routeData.Values["action"] = "Error500";
        Response.StatusCode = 500;

        if (httpException != null) {
            Response.StatusCode = httpException.GetHttpCode();
            Response.TrySkipIisCustomErrors = true;
            switch (Response.StatusCode) {
                case 403:
                    routeData.Values["action"] = "Error403";
                    break;
                case 404:
                    routeData.Values["action"] = "Error404";
                    routeData.Values["message"] = httpException.Message;
                    break;
                case 500:
                    routeData.Values["action"] = "Error500";
                    break;
            }
        }

        IController errorsController = new ErrorController();
        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
        errorsController.Execute(rc);
    }

The problem occurs when I'm not on my (local) development machine (which initially made me think it was customErrors related). When an exception is thrown, Elmah handles the error and logs it correctly. I also end up on the correct error page. However, before ending up on the correct error page, I can see another intermediate exception being logged:

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

ASP.NET is trying to load up a default error page even though I am trying to handle it. Does anyone have any ideas on how to prevent this?

Community
  • 1
  • 1
Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124

5 Answers5

32

Don't call the base.OnException(context); method in your custom error handler that derives from HandleErrorAttribute. You no longer need it because you have implemented a custom error handling in Application_Error.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • That did the trick. After looking at the source code for the HandleErrorAttribute I could see that I needed to skip over some of the base functionality, but I think you're correct that I can just skip calling it altogether and be fine. Thanks for the help. – Justin Helgerson Apr 18 '12 at 16:47
  • 14
    Same issue, different reason. This answer got me looking in the right direction. I was using a with a redirect to a custom ErrorsController. Turned out I also had a FilterConfig.RegisterGlobalFilters() from the original project template, containing a filters.Add(new HandleErrorAttribute()) line. Removing that did it for me. – JonathanN Feb 13 '17 at 16:25
  • @JonathanN Man, you saved me! – Skyblade Feb 20 '17 at 13:21
  • @JonathanN tsm! – William Pereira Feb 04 '21 at 10:53
17

I was having this same issue, but i wasn't executing base.OnException() anywhere. Another possible solution was to remove this from <system.web> in my web.config:

<customErrors mode="On" />
josh-sachs
  • 1,749
  • 2
  • 15
  • 19
4

If you use Elmah.MVC and want to use custom error pages, just change the below value to true in your Web.config:

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

This will keep Elmah logging enabled but stop it from trying to redirect to the default error page.

CapnChaos
  • 101
  • 1
  • 4
3

Somewhere your site is trying to navigate to ~/Error and it can't find it because it doesn't exist.

Trying removing or disabling customErrors in your Web.config. I'm willing to be its set to the default of on and redirect to ~/Error

If you want to post your Web.config (omitting any sensitive information) I can probably help. I've dealt with similar issues with Elmah recently.

Terry
  • 14,099
  • 9
  • 56
  • 84
  • I actually think I know what the issue is, I'm just not sure about the best way to solve it. I have my HTTP responses setup in my Global.asax, but, the OnException override is executing a result of its own. By default that result is the view that it can't find, because I deleted it. So I maybe need to set the `context.Result` property in my OnException override, but I don't know how to do that with my custom errors controller that I have. – Justin Helgerson Apr 17 '12 at 21:53
1

If you use Elmah.mvc, then it will help to change this settings to true:

<add key="elmah.mvc.disableHandler" value="true" />
<add key="elmah.mvc.disableHandleErrorFilter" value="true" />
Peter
  • 489
  • 3
  • 16