Skip to content

Instantly share code, notes, and snippets.

@mikeobrien
Created February 21, 2012 20:55
Show Gist options
  • Save mikeobrien/1878869 to your computer and use it in GitHub Desktop.
Save mikeobrien/1878869 to your computer and use it in GitHub Desktop.
"Conventional" exception handling in ASP.NET MVC
public class ExceptionHandlerFilter : IExceptionFilter
{
...
public void OnException(ExceptionContext context)
{
// Are you kidding me???
var controllerDescriptor = (ControllerDescriptor)new ReflectedControllerDescriptor(context.Controller.GetType());
var actionDescriptor = (ReflectedActionDescriptor)controllerDescriptor.FindAction(context, context.RouteData.Values["action"].ToString());
if (actionDescriptor.MethodInfo.ReturnType == typeof(JsonResult))
{
// Set the status code for JsonResults
}
else
{
// Redirect to error page for ViewResults
}
context.ExceptionHandled = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment