依赖注入接受构造PARAMS设置筛选器属性 [英] Setup filter attribute for dependency injection to accept params in constructor

查看:89
本文介绍了依赖注入接受构造PARAMS设置筛选器属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这个页面一个ninject过滤属性设置。

对于他们来说,他们有:

  .WithConstructorArgumentFromControllerAttribute< LogAttribute>(
      LOGLEVEL属性=> attribute.LogLevel);

第二个参数期待一个 Func键< LogAttribute,对象>回调。他们的实际参数列表的设置如下:

 日志(LogLevel的= Level.Debug)

但我的过滤属性的设置如下:

 公共类AuthAttribute:FilterAttribute {}公共类AuthFilter:个IAuthorizationFilter
{    私人只读IUserService userService;
    私人的String []的作用;    //停留在构造函数也。如何接受PARAMS?
    公共AuthFilter(IUserService userService,PARAMS字符串[]的角色)
    {
        this.userService = userService;
        this.roles =角色;
    }
    公共无效OnAuthorization(AuthorizationContext filterContext)
    {
        抛出新NotImplementedException();
    }
}

不知怎的,这是错误的。因为我希望我的过滤器看起来像:

  [验证(管理,的Contrib)]

我的绑定:

  kernel.BindFilter< AuthFilter>(FilterScope.Controller,0)
            .WhenControllerHas< AuthAttribute>()
            .WithConstructorArgumentFromControllerAttribute< AuthAttribute>(角色,/ *困在这里* /)


解决方案

您需要让角色进入你的属性的属性。

属性:

 公共类AuthAttribute:FilterAttribute
{
  公共字符串[] {角色获得;组; }  公共AuthAttribute(PARAMS字符串[]的角色)
  {
      this.Roles =角色;
  }
}

过滤器:

 公共类AuthFilter:个IAuthorizationFilter
{  私人只读IUserService userService;
  私人只读字符串[]的作用;  公共AuthFilter(IUserService userService,字符串[]的角色)
  {
    this.userService = userService;
    this.roles =角色;
  }  公共无效OnAuthorization(AuthorizationContext filterContext)
  {
    //做东西
  }
}

控制器

  [AuthAttribute(A,B)
   公共类YourController:控制器
   {   }

绑定:

  kernel.BindFilter< AuthFilter>(FilterScope.Controller,0)
            .WhenControllerHas< AuthAttribute>()
            .WithConstructorArgumentFromControllerAttribute< AuthAttribute>(角色,属性= GT; attribute.Roles);

I'm following a ninject filter attribute setup on this page.

For them, they have:

.WithConstructorArgumentFromControllerAttribute<LogAttribute>(
      "logLevel", attribute => attribute.LogLevel);

The second parameter is expecting a Func<LogAttribute, object> callback. Their actual param list is setup as follows:

Log(LogLevel = Level.Debug)

But my filter attribute is setup as follows:

public class AuthAttribute : FilterAttribute { }

public class AuthFilter : IAuthorizationFilter
{

    private readonly IUserService userService;
    private string[] roles;

    //Stuck on the constructor also. How do I accept params?
    public AuthFilter(IUserService userService, params string[] roles)
    {
        this.userService = userService;
        this.roles = roles;
    }
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        throw new NotImplementedException();
    }
}

Somehow this is wrong. Because I want my filter to look like:

[Auth("Admin", "Contrib")]

My bindings:

 kernel.BindFilter<AuthFilter>(FilterScope.Controller, 0)
            .WhenControllerHas<AuthAttribute>()
            .WithConstructorArgumentFromControllerAttribute<AuthAttribute>("roles", /*Stuck here*/)

解决方案

You need to make roles into a property in your attribute.

Attribute:

public class AuthAttribute : FilterAttribute 
{ 
  public string[] Roles { get; set; }

  public AuthAttribute(params string[] roles)
  {
      this.Roles = roles;
  }
}

Filter:

public class AuthFilter : IAuthorizationFilter
{

  private readonly IUserService userService;
  private readonly string[] roles;

  public AuthFilter(IUserService userService, string[] roles)
  {
    this.userService = userService;
    this.roles = roles;
  }

  public void OnAuthorization(AuthorizationContext filterContext)
  {
    //do stuff
  }
}

Controller

   [AuthAttribute("a", "b")]
   public class YourController : Controller 
   {

   }

Binding:

kernel.BindFilter<AuthFilter>(FilterScope.Controller, 0)
            .WhenControllerHas<AuthAttribute>()
            .WithConstructorArgumentFromControllerAttribute<AuthAttribute>("roles", attribute => attribute.Roles);

这篇关于依赖注入接受构造PARAMS设置筛选器属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆