Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

same type action filter on controller and its action method

Tags:

c#

asp.net-mvc

i have defined an action filter like this:

public class InvokeConditionAttribute : ActionFilterAttribute
{
    private ConditionRule conditionRule;

    public InvokeConditionAttribute(ConditionRule conditionRule)
    {
        this.conditionRule = conditionRule;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // check condition rule
    }
}

ConditionRule in parameter is a enum type. then i create a controller like below:

[InvokeCondition(ConditionRule.Condition1, Order = 1)]
public class AttendanceModificationController : Controller
{
    [InvokeCondition(ConditionRule.Condition2, Order = 2)]
    public ActionResult SomeAction()
    {
        //
    }
}

problem is method level filter not invoke.

can define same filter type with different parameter in asp.net mvc?

like image 622
Emad Feiz Avatar asked Jul 12 '26 08:07

Emad Feiz


1 Answers

Decorate your action filter with the AttributeUsage attribute and specify the AllowMultiple = true:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true)]
public class InvokeConditionAttribute : ActionFilterAttribute
{
    ...
}

Now your OnActionExecuting method will be triggered twice for the SomeAction action.

like image 157
Darin Dimitrov Avatar answered Jul 13 '26 22:07

Darin Dimitrov