4

I have a custom error page in my MVC application that's just ~/error/ but when I turn Custom Errors on in the Web.Config like so:

<customErrors mode="On" defaultRedirect="~/error/" />

It only works for 400 errors not server-side 500 errors and instead gives me the following error message on a white page:

"Sorry, an error occurred while processing your request."

How can I just make every single error go to the defaultRedirect page?

Rob
  • 10,004
  • 5
  • 61
  • 91
  • your setting seems just fine...and it should have worked... can you post your MVC code for "~/error/" (I mean the controller/action/view)...? – NiK Jun 10 '12 at 15:51

1 Answers1

3

500 errors should be handled by the MVC application itself. Custom Errors defined by the web.config are for errors other than 500 errors.

Basically, you need to use the RegisterGlobalFilter method (in global.asax) to add a new HandleErrorAttribute to the filter collection (you'll name the view to use for errors in the HandleErrorAttribute), then create a view that takes as its model System.Web.Mvc.HandleErrorInfo. There is no controller that handles these errors or sends the model to the view, so you can't just redirect to your error page.

You can get more information at http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/01/error-handling-and-customerrors-and-mvc3-oh-my.aspx.

EDIT There is an alternative method, where all errors are handled through MVC, shown in How do I display custom error pages in Asp.Net Mvc 3?. Basically, you set up a protected void Application_Error()in your global.asax file to handle all errors, without going through web.config.

Community
  • 1
  • 1
saluce
  • 13,035
  • 3
  • 50
  • 67
  • This seems extremely complex for something that I thought would be quite simple - it's pretty easy to trigger a 500 error in MVC by hacking around with the none Action/Controller parameters of a URL - I'd just like to send someone to a decent error page if they try to do this... – Rob Jun 10 '12 at 04:09
  • @Rob check my edit for an alternative to using web.config customErrors – saluce Jun 10 '12 at 19:09