小编Jam*_*ith的帖子

如何在ASP.NET CORE中使用具有依赖注入的动作过滤器?

我在我的ASP.NET CORE应用程序中使用基于构造函数的依赖注入,我还需要在我的动作过滤器中解决依赖关系:

public class MyAttribute : ActionFilterAttribute
{
    public int Limit { get; set; } // some custom parameters passed from Action
    private ICustomService CustomService { get; } // this must be resolved

    public MyAttribute()
    {
    }

    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        // my code
        ...

        await next();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在控制器中:

[MyAttribute(Limit = 10)]
public IActionResult()
{
    ...
Run Code Online (Sandbox Code Playgroud)

如果我把ICustomService放到构造函数中,那么我就无法编译我的项目了.那么,我如何在动作过滤器中获取接口实例呢?

c# asp.net asp.net-mvc action-filter asp.net-core

14
推荐指数
3
解决办法
1万
查看次数

标签 统计

action-filter ×1

asp.net ×1

asp.net-core ×1

asp.net-mvc ×1

c# ×1