从ASP.NET Core2应用程序中的动态JSON结构方案中获取键值



我正在搜索一种正确的方法来读取我的appsettings.json文件中的asp.net core 2应用程序。我正在共享我的应用程序文件。

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Profiles": {
    "PROFILE1": {
      "Type": "AWS AssumeRole Credential",
      "RoleArn": "arn:aws:iam::11111111111111:role/CrossRole"
    },
    "PROFILE2": {
      "Type": "AWS InstanceProfile Credential",
      "RoleArn": "arn:aws:iam::000000000000:role/Role"
    },
    "AWS_DEBUG": {
      "Type": "AWS Credential MOCK",
      "MockProfile": "PROFILE1",
      "AccessKey": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
      "SecretKey": "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
    }
  }
}

问题在于我想检索所有配置文件名称,然后我将查看其"类型"属性,并了解它必须是其他属性。因此,我可以在代码库中管理这种动态的JSON结构方案。

我的Web应用程序启动脚本正在用IconFiguration对象设置配置,我正在使用它来访问JSON。

       public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

我可以使用configuration [" profiles:profile_scheme_1:type"]来检索值,但我想获取这些配置文件名称,因此用户可以无问题地更改配置文件名称。是否有一种方法可以"在介绍中使用" foreach profilename"(,之后我必须检索该部分进行处理。什么是最好的方法?

var profiles = Configuration.GetSection("Profiles").GetChildren();

,例如,我们可以获取键:

var profilesKeys = profiles.Select(p=>p.Key);

您可以获得的轮廓类型如下:

var profilesTypes = profiles.Select(p => p["Type"]);

最新更新