I have an ASP.NET MVC 4 application. I want to use my custom handle error attribute. Therefor I wrote this class :
public class MyHandleErrorAttribute : HandleErrorAttribute
{
public void OnException(ExceptionContext filterContext)
{
Debug.WriteLine("Error Occured");
}
}
And I have added attribute at controller level :
[MyHandleError(ExceptionType = typeof(Exception),View="ErroView")]
public class MeetingController : Controller
{....}
When an error occures in controller, it can't be handled. If I press F5 in debugger after error, ErrorView is displayed. But Debug.WriteLine("Error Occured");
line never works. What am I doing wrong?
The OnException
method is virtual
but because you don't have the override
modifier there you are hiding and not overriding this method. So your custom method won't get called by the MVC framework.
To correct this you just need to override
the method:
public class MyHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
Debug.WriteLine("Error Occured");
}
}