开发者

ASP.Net MVC Error handling using Action Filters Attributes

开发者 https://www.devze.com 2022-12-15 05:23 出处:网络
I am trying to implement Error handling using Action Filters Attributes as per ScottGu\'s blog My code is as follows:

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

My code is as follows:

[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();
    }

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: Exception was unhandled by user code

NullRefCrash: NullReferenceException was unhandled by user code

and so the unhandled exception is picked up by the Default [Ha开发者_如何学GondleError] which routes to View/Shared/Error.aspx which handles the error.

How do I handle the unhandled exception?


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
{
}
0

精彩评论

暂无评论...
验证码 换一张
取 消