ASP.NET MVC comes with an error-handling feature that enables you to show nice error Views to your users instead of the dreaded Yellow Screen of Death. The most prominently described technique involves attributing either you Controller or a single Controller action, like we see in the AccountController created by the Visual Studio project template.
[HandleError]
public class AccountController : Controller { }
That sure is easy, but I don't like it for a number of reasons:
Another approach is to override Controller.OnException. That solves the DI problem, but not the DRY problem.
Attentive readers may now point out that I can define a base Controller that implements the proper error handling, and require that all my Controllers derive from this base Controller, but that doesn't satisfy me:
First of all, it still violates the DRY principle. Whether I have to remember to apply a [HandleError] attribute or derive from a : MyBaseController amounts to the same thing. I (and all my team members) have to remember this always. It's unnecessary project friction.
The second thing that bugs me about this approach is that I really do favor composition over inheritance. Error handling is a cross-cutting concern, so why should all my Controllers have to derive from a base controller to enable this? That is absurd.
Fortunately, ASP.NET MVC offers a third way.
The key lies in the Controller base class and how it deals with IExceptionFilters (which HandleErrorAttribute implements). When a Controller action is invoked, this actually happens through an IActionInvoker. The default ControllerActionInvoker is responsible for gathering the list of IExceptionFilters, so the first thing we can do is to derive from that and override its GetFilters method:
public class ErrorHandlingActionInvoker :
ControllerActionInvoker
{
private readonly IExceptionFilter filter;
public ErrorHandlingActionInvoker(
IExceptionFilter filter)
if (filter == null)
throw new ArgumentNullException("filter");
}
this.filter = filter;
protected override FilterInfo GetFilters(
ControllerContext controllerContext,
ActionDescriptor actionDescriptor)
var filterInfo =
base.GetFilters(controllerContext,
actionDescriptor);
filterInfo.ExceptionFilters.Add(this.filter);
return filterInfo;
Here I'm simply injecting an IExceptionFilter instance into this class, so I'm not tightly binding it to any particular implementation – it will work with any IExceptionFilter we give it, so it simply serves the purpose of adding the filter at the right stage so that it can deal with otherwise unhandled exceptions. It's pure infrastructure code.
Notice that I first invoke base.GetFilters and then add the injected filter to the returned list of filters. This allows the normal ASP.NET MVC IExceptionFilters to attempt to deal with the error first.
This ErrorHandlingActionInvoker is only valuable if we can assign it to each and every Controller in the application. This can be done using a custom ControllerFactory:
public class ErrorHandlingControllerFactory :
DefaultControllerFactory
public override IController CreateController(
RequestContext requestContext,
string controllerName)
var controller =
base.CreateController(requestContext,
controllerName);
var c = controller as Controller;
if (c != null)
c.ActionInvoker =
new ErrorHandlingActionInvoker(
new HandleErrorAttribute());
return controller;
Notice that I'm simply deriving from the DefaultControllerFactory and overriding the CreateController method to assign the ErrorHandlingActionInvoker to the created Controller.
In this example, I have chosen to inject the HandleErrorAttribute into the ErrorHandlingActionInvoker instance. This seems weird, but it's the HandleErrorAttribute that implements the default error handling logic in ASP.NET MVC, and since it implements IExceptionFilter, it works like a charm.
The only thing left to do is to wire up the custom ErrorHandlingControllerFactory in global.asax like this:
ControllerBuilder.Current.SetControllerFactory(
new ErrorHandlingControllerFactory());
Now any Controller action that throws an exception is gracefully handled by the injected IExceptionFilter. Since the filter is added as the last in the list of ExceptionFilters, any normal [HandleError] attribute and Controller.OnException override gets a chance to deal with the exception first, so this doesn't even change behavior of existing code.
If you are trying this out on your own development machine, remember that you must modify your web.config like so:
<customErrors mode="On" />
If you run in the RemoteOnly mode, you will still see the Yellow Screen of Death on your local box.
Remember Me
a@href@title, b, em, i, strike, strong
Page rendered at Saturday, February 04, 2012 7:58:54 PM (Romance Standard Time, UTC+01:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.