I have a base controller class where I'm overriding to the Controller.OnException handler method in order to provide a generic error handling for certain types of controllers that will inherit from this class (these are API controllers that will return JSON results). The OnException method never gets called when an exception gets raised by the controller. Does anyone see what I am doing wrong, or is there a better way to handle this situation?
Using MVC 1.0
Base Class:
public class APIController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
//This is never called
filterContext.Result =
new JsonResult();
base.OnException(filterContext);
}
}
Inheriting Class:
public class MyController : APIController
{
public AjaxResult ForcedException()
{
throw new SystemException();
}
}
If I understand your question correctly - your Exception must be marked as "handled" in your OnException. Try this:
filterContext.ExceptionHandled = true;