如何识别Controller Action Method中使用的Actionfilter?

时间:2010-06-08 10:25:23

标签: asp.net-mvc action-filter

OnActionExecuting会在某项操作即将执行时触发。

如果我的动作有actionfilter


[myCustomActionFilter]
public ActionResult MyAction()
{
//implementation
}

是否可以确定(OnActionExecuting事件内)某个操作已应用myCustomActionFilter

2 个答案:

答案 0 :(得分:2)

上述方法区分大小写。

public void OnActionExecuting(ActionExecutingContext filterContext)
{
    var actionDescriptor = filterContext.ActionDescriptor;
    if (actionDescriptor.IsDefined(typeof(myCustonActionFilter), true))
    {
        var attributeInstance = (myCustomActionFilter) actionDescriptor.GetCustomAttributes(typeof(myCustomActionFilter), true);

    }   
}

答案 1 :(得分:1)

     public void OnActionExecuting(ActionExecutingContext filterContext)
            {
                var controllerType = filterContext.Controller.GetType();
                var actionMethod = controllerType.GetMethod(filterContext.ActionDescriptor.ActionName);
                if (actionMethod.IsDefined(typeof(myCustomActionFilter),true))
                {
                    var attributeInstance =
                        (myCustomActionFilter) actionMethod.GetCustomAttributes(typeof (myCustomAct   
ionFilter), true);
        }