Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Page in asp.net mvc 3

I have a new asp.net mvc 3 website with the razor engine and am trying to get the site ready for production where I cannot have the Yellow Screen of Death come up. In webforms this is easy, just turn on customErrors in the web.config and you are done.

To test I setup a test controller method as such:

    public ActionResult Ex()
    {
        throw new InvalidOperationException();
    }

I expected the error view (/Views/Shared/Error.cshtml) to be displayed instead I get the Yellow Screen of Death with the message "Operation is not valid due to the current state of the object." I tried turning on customErrors in the web.config and it still does not work. I am calling the RegisterGlobalFilters method in the Global.asax but have also tried applying the HandleError attribute directly.

Thanks for your help.

like image 252
Dave Avatar asked Apr 27 '11 15:04

Dave


1 Answers

Turn on the custom errors and route them to an Error controller.

<customErrors mode="On" defaultRedirect="~/Error/Unknown">
  <error statusCode="403" redirect="~/Error/NoAccess" />
  <error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>

The controller needs the actions Unknown, NoAccess, and NotFound. Each will need to return a view.

like image 104
37Stars Avatar answered Sep 27 '22 21:09

37Stars