Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC InvalidOperationException with custom error pages

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.

like image 352
Ryan Sampson Avatar asked Sep 01 '11 01:09

Ryan Sampson


2 Answers

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.

like image 198
Eranga Avatar answered Sep 22 '22 06:09

Eranga


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?

like image 30
Derek Morrison Avatar answered Sep 21 '22 06:09

Derek Morrison