I am trying to implement Custom Error handling via Action Filter Attributes.
My code is as follows:
[HandleError (Order = 2)]
[HandleError (Order = 1, ExceptionType = typeof(NullReferenceException), View = "CustomError")]
public class ArticlesController : Controller
{
public object OhDearACrash()
{
throw new Exception("Oh Dear");
}
public ActionResult NullRefCrash()
{
throw new NullReferenceException();
}
public ActionResult ThrowNotImplemented()
{
throw new NotImplementedException();
}
OhDearACrash and ThrowNotImplemented are both picked up by [HandleError] which renders the error message via Error.aspx located in Views/Shared.
For example with OhDearACrash:
Message = <%= ViewData.Model.Exception.Message %>
renders
Message = Oh Dear
NullRefCrash is picked up by the [HandeError] that deals with an ExceptionType = typeof(NullReferenceException).
When CustomError attempts to render the error message using
Message = <%= ViewData.Model.Exception.Message %>
the ViewData.Model is null and an exception is raised
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
To create CustomError.aspx I copied Error.aspx and pasted to my newly created Views/Error and renamed to CustomView.aspx.
Since Error.aspx and CustomError.aspx are essentially the same how is this occuring?
Edit:
I created a Test Project that only contains the above and the CustomError.aspx view works perfectly fine - Is there anyway to debug my existing project to find the issue?