Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.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# ASP.NET Core 2.1在函数外使用变量_C#_Asp.net_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# ASP.NET Core 2.1在函数外使用变量

C# ASP.NET Core 2.1在函数外使用变量,c#,asp.net,asp.net-core,asp.net-core-mvc,C#,Asp.net,Asp.net Core,Asp.net Core Mvc,我有这个打字机 [TypeFilter(typeof(ValidateRolesFilter), Arguments = new object[] { configuration["key"], RoleGlobals.SystemAdministrator })] public IActionResult About() { return View(); } 在上面的类构造函数中,我声明了如下配置 public HomeController(IApplicationUse

我有这个打字机

[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object[] {
      configuration["key"], RoleGlobals.SystemAdministrator
})]
public IActionResult About()
{
    return View();
}
在上面的类构造函数中,我声明了如下配置

public HomeController(IApplicationUserClient getUserClient, IConfiguration configuration)
{
    this.getUserClient = getUserClient;
    this.configuration = configuration;
}
然而,当我试图在我的typefilter中声明配置[“Item”]时,它不允许我这样做。我只能在函数内部执行

我该怎么做才能使其在函数之外使用配置变量?我尝试将其设置为常量,但它不起作用,因为它读取appsettings.json

[TypeFilter(typeof(ValidateRolesFilter), Arguments = new object[] {
  configuration["key"], RoleGlobals.SystemAdministrator})]
它上面的那条线叫做A。属性是可在运行时读取的编译时指令

变量
configuration[“key”]
在运行时加载。因此编译器将失败

这是另一个设计。在构造函数中实例化
ValidateRolesFilter

public HomeController(IApplicationUserClient getUserClient, IConfiguration configuration)
{
    this.getUserClient = getUserClient;
    this.configuration = configuration;

    // This is just a guess; I have no idea what this object is
    this.canAccessAbout = new ValidateRolesFilter(configuration["key"], RoleGlobals.SystemAdministrator);
}
然后在“关于”方法中:

public IActionResult About()
{
    // again, this is just a guess 
    if (this.canAccessAbout.Validate())
    {
        return View();
    }
    else
    {
        // redirect them or display error page
    }
}

你在功能之外是什么意思?你的意思是在你班上的另一个方法里吗?我已经编辑了我的问题。如果您看到我的typefilter所在的位置,那么它位于About函数/方法之外。我无法从那里访问配置变量。您试图用属性中的
配置[“key”]
实现什么?键值应该做什么?我正在尝试从appsettings.json读取一个键。typefilter位于泛型类中,因此其他应用程序将使用它。不幸的是,这意味着我不能在过滤器内部声明密钥。有没有办法将appsettings.json项推送到这个过滤器中?