Let's say I put the following code somewhere in a Master page in my ASP.NET MVC site:
throw new ApplicationException("TEST");
Even with a [HandleError] attribute placed on my controller, this exception still bubbles up. How can I deal with errors like this? I would like to be able to route to an Error page and still be able to log the exception details.
What is the best way to deal with something like this?
Edit: One solution I was considering would be to add a a new controller: UnhandledErrorController. Can I put in an Application_Error method in Global.asax and then redirect to this controller (where it decides what to do with the exception)?
Note: the defaultRedirect in the customErrors web.config element does not pass along the exception info.
You can create a Filter that looks for an Exception in the
OnActionExecuted
method:Then you can put
[WatchException]
on a Controller or Action Method, and it will let log exceptions. If you have a lot of Controllers, that might be tedious, so if you have a common Base Controller you can overrideOnActionExecuted
there and do the same thing. I prefer the filter method.As far as what page to display, you'll need to create a customErrors section in your web.config and set it up for whatever status codes you want to handle.
Example:
As far as logging exceptions, I would recommend using ELMAH. It integrates nicely with ASP.NET MVC sites.
As MVC is built on top of asp.net you should be able to define a global error page in web.config, just like you could in web forms eg.
Enable customErrors:
and redirect to a custom error controller: