Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Controller.OnException not being called

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();
        }
}
like image 907
Stephen franklin Avatar asked Jun 06 '09 18:06

Stephen franklin


1 Answers

If I understand your question correctly - your Exception must be marked as "handled" in your OnException. Try this:

filterContext.ExceptionHandled = true;
like image 84
eu-ge-ne Avatar answered Oct 13 '22 22:10

eu-ge-ne