我正在尝试通过动作过滤器属性实现自定义错误处理。
我的代码如下:
[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 和 ThrowNotImplemented 都被 [HandleError] 拾取,它通过位于 Views/Shared 中的 Error.aspx 呈现错误消息。
以 OhDearACrash 为例:
Message = <%= ViewData.Model.Exception.Message %>
渲染
Message = Oh Dear
NullRefCrash 由处理 ExceptionType = typeof(NullReferenceException) 的 [HandeError] 拾取。
当 CustomError 尝试使用
Message = <%= ViewData.Model.Exception.Message %>
ViewData.Model 为 null 并引发异常
System.NullReferenceException was unhandled by user code
Message="Object reference not set to an instance of an object."
为了创建 CustomError.aspx,我复制了 Error.aspx 并粘贴到我新创建的 Views/Error 中,并重命名为 CustomView.aspx。
由于 Error.aspx 和 CustomError.aspx 本质上是相同的,这是怎么发生的?
编辑:
我创建了一个仅包含上述内容的测试项目,并且 CustomError.aspx 视图工作得非常好 - 无论如何要调试我现有的项目以找到问题?