加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用asp.net mvc操作过滤器的奇怪行为AttributeU

发布时间:2020-12-16 03:15:17 所属栏目:asp.Net 来源:网络整理
导读:我有一个具有以下签名的动作过滤器 [AttributeUsage(AttributeTargets.Method,AllowMultiple = true)]public class UnitOfWorkAttribute : ActionFilterAttribute 根据MSDN: The AllowMultiple property indicates whether multiple instances of your attr
我有一个具有以下签名的动作过滤器
[AttributeUsage(AttributeTargets.Method,AllowMultiple = true)]
public class UnitOfWorkAttribute : ActionFilterAttribute

根据MSDN:

The AllowMultiple property indicates whether multiple instances of
your attribute can exist on an element. If set to true,multiple
instances are allowed; if set to false (the default),only one
instance is allowed.

在MVC中,行为似乎有点奇怪.当我用这个属性装饰一个动作时,我发现过滤器的OnActionExecuting方法被执行了两次.

过滤器只在操作上声明,而不是在控制器上声明,我已经清除了任何全局过滤器.有人可以解释这个行为吗?

解决方法

我遇到同样的问题. (我安装了一个全局过滤器(只有一次),并发现它的IActionFilter和IResultFilter方法每个请求被调用两次.传递给这些方法的filterContext.HttpContext对象对于两个调用是完全相同的.

原来是由于在视图中使用了Html.Action.调用Html.Action会显示(从调用堆栈中)调用子动作方法(在处理初始动作方法期间),并为这两个调用过滤器.

您可以通过检查filterContext.IsChildAction属性来检测这种情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读