【问题标题】:ActionFilterAttribute is getting called for all actions正在为所有操作调用 ActionFilterAttribute
【发布时间】:2012-01-03 09:32:26
【问题描述】:

也许我误解了ActionFilterAttribute的意思,但我现在的情况如下:

public class MyCustomAttribute : ActionFilterAttribute
{
  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    //do something useful here
  }
}

然后在我的 Home 控制器中,我有以下操作方法:

public class HomeController : Controller
{
  public ActionResult Index()
  {
    return View();
  }

  [MyCustom]
  public ActionResult Test()
  {
    return View();
  }
}

我现在期望的是,当我尝试访问 /Home/Test 时,OnActionExecuting 会被触发,但当我尝试访问 /Home/Index 时不会触发。

但是,这两种操作方法都会触发它。我还在OnActionExecuting 内部验证了实际上正在调用Index 操作。

是否有可能只在调用标记有该属性的操作方法时才调用OnActionExecuting

【问题讨论】:

  • 删除 global.asax 中存在的那个。

标签: .net asp.net-mvc-3 actionfilterattribute


【解决方案1】:

检查MyCustomAttribute 是否存在于Global.asaxGlobalFilters 集合中。

【讨论】:

  • 解决了。在我把它变成一个过滤器属性之前,它开始是一个全局过滤器。非常感谢!
  • @ØyvindKnobloch-Bråthen 你能否详细说明你做了什么来解决它?我有同样的问题,如果我将它添加到全局过滤器,它会在每个操作上执行。如果我不添加它,那么它永远不会执行。我应该在哪里添加它?我没有看到要添加的其他类型的过滤器。
  • 您不需要在任何特殊的地方添加它。如果你调试它,你能检查它是否进入了 OnActionExecuting 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 2014-06-30
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多