Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在动作过滤器中扩展依赖性注入?_C#_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# 如何在动作过滤器中扩展依赖性注入?

C# 如何在动作过滤器中扩展依赖性注入?,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,启动时,我已设置我的操作过滤器 services.AddScoped<CheckThisAttribute>(); 并在控制器上通过 [ServiceFilter(typeof(CheckThisAttribute))] public class MyController : Controller { } 正如您可能已经注意到的,我在构造函数中将name参数设置为可选的。 如果我不这样做,我将得到运行时错误。上下文是通过DI传递的,但名称不是 我是否可以扩展此操作筛选器,以便还可

启动时,我已设置我的操作过滤器

services.AddScoped<CheckThisAttribute>();
并在控制器上通过

[ServiceFilter(typeof(CheckThisAttribute))]
public class MyController : Controller
{
}
正如您可能已经注意到的,我在构造函数中将name参数设置为可选的。 如果我不这样做,我将得到运行时错误。上下文是通过DI传递的,但名称不是

我是否可以扩展此操作筛选器,以便还可以从控制器传递名称? 差不多

[ServiceFilter(typeof(CheckThisAttribute(name="Something")))]

使用ServiceFilterAttribute无法实现该结果

相反,请尝试TypeFilterAttribute,它将如下所示:

[TypeFilter(typeof(CheckThisAttribute),
    Arguments = new object[] { "Method 'Hi' called" })]
以下是文档的链接:

[TypeFilter(typeof(CheckThisAttribute),
    Arguments = new object[] { "Method 'Hi' called" })]