本文介绍了ASP.Net MVC使用错误处理措施筛选器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现错误使用Action过滤器属性的处理按照ScottGu的的

I am trying to implement Error handling using Action Filters Attributes as per ScottGu's blog

我的code是如下:

[HandleError]
[HandleError(ExceptionType = typeof(NullReferenceException), View = "CustomError")]
public class ArticlesController : Controller
{
    public object OhDearACrash()
    {
        throw new Exception("Oh Dear");
    }

    public object NullRefCrash()
    {
        throw new NullReferenceException();
    }

我遇到了在那里我从来没有能够作为我收到一个异常时,抛出异常击中CustomError查看问题

I am encountering an issue where I am never able to hit the CustomError view as I receive an exception when the exception is thrown

OhDearACrash:异常是由用户code

OhDearACrash: Exception was unhandled by user code

NullRefCrash:NullReferenceException异常是由用户code

NullRefCrash: NullReferenceException was unhandled by user code

等未处理的异常是由默认拿起[的HandleError]查看哪些路由/共享/ Error.aspx它处理错误。

and so the unhandled exception is picked up by the Default [HandleError] which routes to View/Shared/Error.aspx which handles the error.

我如何处理未处理的异常?

How do I handle the unhandled exception?

推荐答案

动作过滤器逐一执行。在你的情况,问题很可能是通用的HandleError行为过滤器是具体的人之前执行。

The action filters are executed one by one. In your case, the problem is probably that the generic HandleError action filter is executed before the specific one.

您可以通过设置过滤器作用的秩序属性影响执行的顺序:

You can influence the order of execution by setting the 'Order' property of your action filter:

[HandleError(Order = 2)]
[HandleError(Order = 1, ExceptionType = typeof(NullReferenceException), View = "CustomError")]
public class ArticlesController : Controller
{
}

这篇关于ASP.Net MVC使用错误处理措施筛选器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:00