ASP.Net MVC使用错误处理措施筛选器属性

编程入门 行业动态 更新时间:2024-10-10 15:24:31
本文介绍了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使用错误处理措施筛选器属性

本文发布于:2023-10-30 18:26:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   错误   措施   ASP   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!