views:

33

answers:

2

OnActionExecuting triggers when an action is about to execute.

If my action has actionfilter


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

Is it possible to determine (inside the OnActionExecuting event) that an action has myCustomActionFilter applied into it?

+1  A: 
     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);
        }
Sergey Mirvoda
+2  A: 

The method above is case sensitive.

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

    }   
}
Ivan Butsyrin