Custom Error Pages in ASP.NET MVC
Wednesday, August 26, 2009 at 12:31AM |
Kevin Rohrbaugh Disclaimer: This is a bit of a reminder post to myself, but it may help others as well.
When writing an ASP.NET MVC application, the need to gracefully handle errors (including HTTP 404 errors) will come up sooner or later. As with other aspects of MVC, an important thing to remember is that it’s just a new layer of abstraction on top of ASP.NET so the classic ways of handling errors are all present in MVC, as well as a few new options.
Once again, though, others have already done a good job documenting the various approaches, so I’ll link to that content rather than repeat it:
- Barry Dahlberg’s ASP.NET MVC Custom Error Pages
- Several StackOverflow questions on the topic
- ELMAH – this isn’t directly related to graceful error handling, but it’s worth looking into.
These should give you some sense to the possible approaches for handling errors in MVC. I’ve found Barry’s post particularly helpful for my needs so far, with one exception (which is the self-reminder part of this post). Barry outlined a way to disable IIS7 custom errors using the IIS7 administration interface, but, depending on your hosting environment, you may not have access to this UI. Fortunately, you can change IIS’ error mode using the system.webServer/httpErrors node.
To disable IIS’ custom errors and use detailed error pages (those which you’ve defined for you application) add the following to the system.webServer section in your application’s root Web.config file.
<system.webServer> <httpErrors errorMode="Detailed" /> ... other settings ... </system.webServer>Of course, this only works on IIS7+, but you should now see the error pages you’ve defined for your application instead of the IIS ones.

Reader Comments