Created
February 21, 2012 20:55
-
-
Save mikeobrien/1878869 to your computer and use it in GitHub Desktop.
"Conventional" exception handling in ASP.NET MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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