简体   繁体   English

ASP.NET Core 2.1如何调用引用一个类的过滤器

[英]ASP.NET Core 2.1 How to call filter that refers to a class

I have this action filter that I want to call from a function. 我有一个要从函数中调用的动作过滤器。 However, within this filter I am referring to another class called ValidateRoleClient. 但是,在此过滤器中,我指的是另一个名为ValidateRoleClient的类。 How can I do a service injection? 如何进行服务注入? Or call this class in my function properly? 还是在我的函数中正确调用此类?

public class RolesFilterAttribute : ActionFilterAttribute
{
    public ValidateRoleClient validateRoleClient;
    public string Role { get; set; }

    public RolesFilterAttribute(ValidateRoleClient validateRoleClient)
    {
        this.validateRoleClient = validateRoleClient;
    }

    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (context.HttpContext.Request.Cookies["Token"] != null || context.HttpContext.Request.Cookies["RefreshToken"] != null)
        {
            TokenViewModel tvm = new TokenViewModel
            {
                Token = context.HttpContext.Request.Cookies["Token"],
                RefreshToken = context.HttpContext.Request.Cookies["RefreshToken"]
            };
            ValidateRoleViewModel vrvm = new ValidateRoleViewModel
            {
                Role = Role,
                Token = tvm
            };
            validateRoleClient.ValidateRole(vrvm);
        }
    }
}

This is how I refer to the filter in my function 这就是我在函数中引用过滤器的方式

[RolesFilter]
public IActionResult About()
{
    return View();
}

The current error I get from this is 我从中得到的当前错误是

Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'validateRoleClient' of 'RolesFilterAttribute.RolesFilterAttribute(ValidateRoleClient)' 严重性代码说明项目文件行抑制状态错误CS7036没有给出与“ RolesFilterAttribute.RolesFilterAttribute(ValidateRoleClient)”的所需形式参数“ validateRoleClient”相对应的参数。

You could wrap your attribute inside a FilterFactory like so: 您可以将属性包装在FilterFactory如下所示:

public class RolesFilterAttribute : Attribute, IFilterFactory
{
    public string Role { get; set; }       

    public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
    {
        return new RolesFilterAttributeImplementation(
            serviceProvider.GetRequiredService<ValidateRoleClient>(),
            Role
        );
    }

    private class RolesFilterAttributeImplementation : ActionFilterAttribute
    {
        private readonly ValidateRoleClient validateRoleClient;
        private readonly string role;

        public RolesFilterAttributeImplementation(ValidateRoleClient validateRoleClient, string role)
        {
            this.validateRoleClient = validateRoleClient;
            this.role = role;
        }

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.HttpContext.Request.Cookies["Token"] != null || context.HttpContext.Request.Cookies["RefreshToken"] != null)
            {
                TokenViewModel tvm = new TokenViewModel
                {
                    Token = context.HttpContext.Request.Cookies["Token"],
                    RefreshToken = context.HttpContext.Request.Cookies["RefreshToken"]
                };
                ValidateRoleViewModel vrvm = new ValidateRoleViewModel
                {
                    Role = role,
                    Token = tvm
                };
                validateRoleClient.ValidateRole(vrvm);
            }
        }
    }

    public bool IsReusable => false;
}

Of course the ValidateRoleClient service must first be configured to be injected. 当然,必须首先将ValidateRoleClient服务配置为注入。

You can then use the RolesFilterAttribute attribute as you normally would. 然后,您可以像RolesFilterAttribute一样使用RolesFilterAttribute属性。

And you can read more about IFilterFactory here: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-2.1#ifilterfactory 您可以在这里阅读有关IFilterFactory更多信息: https : IFilterFactory ? IFilterFactory = IFilterFactory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM