简体   繁体   English

带有参数和 DI 的 ASP.Net Core 过滤器

[英]ASP.Net Core filters with arguments and DI

There is a way to make filter with arguments and DI in ASP.NET Core?有没有办法在 ASP.NET Core 中使用参数和 DI 进行过滤?

My working TestFilterAttribute with TestFilterFilter and DI without arguments:我的工作TestFilterAttributeTestFilterFilter和 DI 不带参数:

public class TestFilterAttribute : TypeFilterAttribute
{
    public TestFilterAttribute() : base(typeof(TestFilterFilter))
    {
    }

    private class TestFilterFilter : IActionFilter
    {
        private readonly MainDbContext _mainDbContext;

        public TestFilterFilter(MainDbContext mainDbContext)
        {
            _mainDbContext = mainDbContext;
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {


        }

        public void OnActionExecuted(ActionExecutedContext context)
        {


        }
    }
}

And want simply use [TestFilter('MyFirstArgument', 'MySecondArgument')] with agruments instead [TestFilter] without arguments并且想要简单地使用[TestFilter('MyFirstArgument', 'MySecondArgument')]和 agruments 而不是[TestFilter]不带参数

There is, if you look at the source.有,如果你看一下来源。 Never tried though, so you got to try it yourself (can't test it at work and internet issues at home).不过从未尝试过,因此您必须自己尝试(无法在工作中进行测试,在家中无法测试互联网问题)。

The documentation names one of it, for untyped parameters:文档为无类型参数命名了其中之一:

[TypeFilter(typeof(AddHeaderAttribute),
    Arguments = new object[] { "Author", "Steve Smith (@ardalis)" })]
public IActionResult Hi(string name)
{
    return Content($"Hi {name}");
}

The xmldoc of TypeFilterAttribute says TypeFilterAttribute的 xmldoc 说

    /// <summary>
    /// Gets or sets the non-service arguments to pass to the <see cref="ImplementationType"/> constructor.
    /// </summary>
    /// <remarks>
    /// Service arguments are found in the dependency injection container i.e. this filter supports constructor
    /// injection in addition to passing the given <see cref="Arguments"/>.
    /// </remarks>
    public object[] Arguments { get; set; }

Alternatively, you can add properties to your TestFilterAttribute and assign them in the constructor, but this only works if the parameter is mandatory and hence set via constructor或者,您可以将属性添加到您的TestFilterAttribute并在构造函数中分配它们,但这仅在参数是必需的并因此通过构造函数设置时才有效

public class TestFilterAttribute : TypeFilterAttribute
{
    public TestFilterAttribute(string firstArg, string secondArg) : base(typeof(TestFilterFilter))
    {
        this.Arguments = new object[] { firstArg, secondArg }
    }

    private class TestFilterFilter : IActionFilter
    {
        private readonly MainDbContext _mainDbContext;
        private readonly string _firstArg;
        private readonly string _secondArg;

        public TestFilterFilter(string firstArg, string secondArg, MainDbContext mainDbContext)
        {
            _mainDbContext = mainDbContext;
            _firstArg= firstArg;
            _secondArg= secondArg;
        }

        public void OnActionExecuting(ActionExecutingContext context) { }

        public void OnActionExecuted(ActionExecutedContext context) { }
    }
}

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

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