10

I have custom error pages setup using

<customErrors mode="On" defaultRedirect="~/Home/Error">
    <error statusCode="404" redirect="~/Home/PageNotFound" />
</customErrors>

I created a page that throws and exception and I get redirected to the correct error pages.

However I am noticing these errors in elmah on the production webserver:

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

Why is it looking for the error page elsewhere? I deleted ~/Views/Shared/Error.cshtml and added my custom error page at ~/Home/Error since i specified a new default in my config file.

Any ideas?

Thanks.

Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Ryan Sampson
  • 6,717
  • 12
  • 47
  • 55
  • Since `customErrors` is something ASP.NET deals with (not ASP.NET MVC), it's not the best tool for the job. Richard Dingwall has an excellent post on this: http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/ – Bryan Menard Sep 01 '11 at 01:48

5 Answers5

29

MVC projects by default adds the HandleErrorAttribute in the Global.asax.cs file

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

This filter is executed when an unhandled exception is thrown. It sets the view as Error. Hence MVC runtime tries to render that view. But in your case, there is no such view. So it again throws another exception which is handled by ASP.NET runtime and shows your error page that you have configured in Web.Config file.

You can create your own exception filter and register it.

Eranga
  • 32,181
  • 5
  • 97
  • 96
  • Thanks you are right that seems to be exactly what is happening. I placed the default error view back and threw an exception, that error about not find the default view is gone. However elmah does not log the exception. I am fine with using the default view, but how come elmah doesn't log the errors? It logs 404 just fine, but not the server 500 errors. – Ryan Sampson Sep 01 '11 at 03:16
  • 2
    @Aros - You need to implement a special version of the HandleErrorAttribute for Elmah. You can find an exmaple here: http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx – Erik Funkenbusch Sep 01 '11 at 03:41
  • Thanks for now I commented out the line in the answer, and am relying on custom errors redirect from web.config. I will use the answer here if that causes problems. http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute/779961#779961 Anyone know why I would not want to comment that line of code out? – Ryan Sampson Sep 01 '11 at 16:29
  • 3
    _This filter is executed when an unhandled exception is thrown. It sets the view as Error._ This is a very valuable piece of information. Thanks a lot. – Veysel Ozdemir Mar 06 '13 at 14:24
5

I ended up taking out the registration of HandleErrorAttribute in Global.asax and just using the <customErrors> section. ELMAH now properly logs errors, and I'm able to specify custom error pages.

Am I missing something?

Derek Morrison
  • 5,456
  • 4
  • 31
  • 24
1

You can also make sure that the HandleErrorAttribute is not registered by removing it from the global filters, using the Remove method:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Remove(new HandleErrorAttribute());
        /* ... your other filters */
    }
}
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
0

For future readers of this post, note that Elmah adds its own HandleErrorAttribute so is also expecting the Error.cshtml view. Note that I also installed the Elmah.MVC NuGet package but that is just used to set it up properly.

Yes, just noticed on nuget.org that Elmah.MVC is responsible for adding the HandleErrorAttribute:

Painless integration of ELMAH functionality into ASP.NET MVC Application. Just drop the package inside your ASP.NET MVC application and access /elmah URL. It will also install global HandleError filter, that guarantees all unhandled errors are logged (even if customError turned "On").

richinator38
  • 165
  • 1
  • 18
0

To disable ELMAH's HandleErrorAttribute add the following line to the appSettings section of your Web.Config file:

<!-- language: lang-xml -->
<add key="elmah.mvc.disableHandleErrorFilter" value="true" />
Paul R
  • 1