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

.net – 为所有操作调用ActionFilterAttribute

发布时间:2020-12-16 06:30:27 所属栏目:asp.Net 来源:网络整理
导读:也许我误解了ActionFilterAttribute的观点,但我现在所拥有的是以下内容: public class MyCustomAttribute : ActionFilterAttribute{ public override void OnActionExecuting(ActionExecutingContext filterContext) { //do something useful here }} 然后
也许我误解了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();
  }
}

我现在期望OnActionExecuting在我尝试访问/ Home / Test时被触发,但在我尝试访问/ Home / Index时却没有.

但是,它会被两个动作方法触发.我还在OnActionExecuting内部验证了实际上正在调用的Index动作.

当您调用标记有属性的操作方法时,是否有可能只调用OnActionExecuting?

解决方法

检查Global.asax中的GlobalFilters集合中是否存在MyCustomAttribute.

(编辑:李大同)

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

    推荐文章
      热点阅读