Handling Exceptions that happen in a asp.net MVC Controller Constructor

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2010-04-22T22:02:58Z Indexed on 2010/04/22 23:23 UTC
Read the original article Hit count: 255

What's the best way to handle exceptions that happen from within a controller's constructor?

All I can think of to do is use Application_OnError() or put a try/catch in my ControllerFactory.

Neither of these solutions seem ideal. Application_OnError is to broad - I have some non-mvc content in the site that has its own error handling.

Using a try/catch block seems kinda hacky.

If I'm serving different content type -html/text/json/rss.... I would like to be able to handle the exception from within the action method instead of having to write all kinds of conditions to determine what kind of error message to serve.

Am I missing something here, or has anyone else dealt with this?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about asp.net-mvc