Как определить Actionfilter, используемый в методе действия контроллера? - PullRequest
1 голос
/ 08 июня 2010

OnActionExecuting срабатывает, когда действие готовится к выполнению.

Если у моего действия есть actionfilter


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

Можно ли определить (внутри события OnActionExecuting), чток действию применили myCustomActionFilter?

Ответы [ 2 ]

2 голосов
/ 23 июня 2010

Метод выше чувствителен к регистру.

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

    }   
}
1 голос
/ 22 июня 2010
     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);
        }
...