Ninject Binding Attribute to Filter with Constructor Arguments

江枫思渺然 提交于 2019-11-27 14:41:15

I have figured it out (thanks to Remo's directions and documentation).

Use the appropriate .WithConstructorArgument extension whether you are binding to a Controller or Action filter. For example binding my action filter looks like this:

kernel.BindFilter<AuthorizationFilter>(FilterScope.Action, 0)
      .WhenActionMethodHas<RequireRolesAttribute>()
      .WithConstructorArgumentFromActionAttribute<RequireRolesAttribute>("requiredRoles", o => o.RequiredRoles);

Once I understood the Func<> signature, it all became clear. The best way I found to handle this was to

  1. make the extension type-specific for my attribute

    .WithConstructorArgumentFromActionAttribute<TAttribute>()
    
  2. fetch the value from the callback object (your attribute) via lambda:

    ("argumentName", o => o.PropertyName)
    
kdaShivantha

and to bring the BindFilter extension method into scope don't forget to add;

using Ninject.Web.Mvc.FilterBindingSyntax;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!