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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 使用asp.net mvc操作过滤器的奇怪行为AttributeUsage前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个具有以下签名的动作过滤器
[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属性来检测这种情况.

原文链接:https://www.f2er.com/aspnet/249911.html

猜你在找的asp.Net相关文章