Custom Error View and Elmah

I added Elmah you my MVC 4 web portal that I had been working on. I had Custom Error controller and view configured to handle the general public error messages, which worked perfectly.

I noticed that on a live deployment that I received an occasional exception

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

The problem occurs because Elmah 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 un-handled exception is thrown. It sets the view as Error. The MVC runtime tries to render that view. But in this case, there is no such view. So it again throws another exception which is handled by ASP.NET runtime.

There are two solutions to this:

1) Add an Error.cshtml view in the Shared folder to display the exception thrown by the runtime.

2) Add/edit the key in web.comnfig appSettings

    <add key="elmah.mvc.disableHandleErrorFilter" value="true" />
This allows the exception to propagate to the configured Error controller using the existing routing rules.

Add comment