Problem with Ninject and MVC3 Action Filter depending on the action on the controller and action

Recently, I decided to remove a bunch of action level filters in the controller and replace them with one controller level filter.

Now I get this error message.

Error activating LogActionFilter
More than one matching bindings are available.
Activation path:
 1) Request for LogActionFilter

Suggestions:
 1) Ensure that you have defined a binding for LogActionFilter only once.

I am sure the error is due to the fact that the action filter is linked twice, like what I changed. However, when I review the documentation here , I see that it points / does the same. So I'm really not sure what I'm doing wrong.

My sample controller

[LogAction]
public class SomeController : Controller
{
    public ActionResult SomeAction()
    { 

    }
}

My registration code

public static void RegisterFilters()
{
    Kernel.BindFilter<LogActionFilter>(FilterScope.Controller, 0)
    .WhenControllerHas<LogActionAttribute>();

    Kernel.BindFilter<LogActionFilter>(FilterScope.Action, 0)
        .WhenActionMethodHas<LogActionAttribute>();
}
+3
3

, LogActionAttribute.

+4

( , , .)

, . :

public class MyAuthorizationFilter : IAuthorizationFilter
{
   /* call base ctor */
}

public class MyControllerAuthorizationFilter : MyAuthorizationFilter
{
   /* call base ctor */
}

public class MyActionAuthorizationFilter : MyAuthorizationFilter
{
}

:

this.BindFilter<MyControllerAuthorizationFilter>(FilterScope.Controller, 0)
            .WhenControllerHas<MyAttribute>()
            .WithConstructorArgumentFromControllerAttribute<ProtectedAttribute>(/*...*/) ;

this.BindFilter<MyActionAuthorizationFilter>(FilterScope.Action, 0)
            .WhenActionMethodHas<MyAttribute>()
            .WithConstructorArgumentFromActionAttribute<ProtectedAttribute>(/*...*/) ;

WithConstructorArgumentFrom [ Controller/Action] " " ( ).

+3

Better workaround. In fact, I use this in the new version, instead of having two bindings for controllers and actions.

this.BindFilter<MyFilter>(FilterScope.Global, int.MinValue)
    .When((controllerContext, actionDescriptor) =>
                                                 controllerContext
                                                .Controller
                                                .GetType()
                                                .GetCustomAttributes(typeof(MyAttribute),true)
                                                .Length > 0 
    || actionDescriptor.GetCustomAttributes(typeof(MyAttribute), true).Length > 0);
+1
source

All Articles