【问题标题】:Iterate through and read all appsettings keys in asp.net core遍历并读取 asp.net 核心中的所有 appsettings 键
【发布时间】:2020-06-08 21:05:48
【问题描述】:

我在我的 asp.net core 3.0 web api 项目的 appsettings.json 文件中添加了一些配置,该文件如下所示:

{
  "Logging":{..},
  "AllowedHosts": "*",
  "Section1": {
     "Key1": "Value1",
     "Key2": "Value2",
     ....
  }
}

我想遍历此特定部分 Section1 中的所有键并对它们执行一些操作。我尝试了以下方法,但它不起作用:

foreach (var key in ConfigurationManager.AppSettings.AllKeys)
                {
                    var key = ConfigurationManager.AppSettings["Key1"];
                    // perform some action
                }

ConfigurationManager.AppSettings 不包含任何内容,如下面的屏幕截图所示:

我还需要做什么才能完成这项工作?

我尝试过var v = this._configuration.GetSection("Section1").GetSection("Key1");,其中_configuration 属于IConfiguration 类型,它按预期工作。但同样,就像我提到的那样,我不想要这个,而是我想遍历 appsettings 中的所有键列表并对它们执行一些操作。

任何帮助都会很棒。

【问题讨论】:

  • .NET Core 中没有这样的类
  • 文档中的Configuration section 解释了如何创建和加载配置,如何将所有 数据转换为键/值对,如何使用@ 查找部分和值987654331@ 或 GetValue 以及如何从值中获取具体类型
  • 我已经尝试过使用 GetSection 和 GetValue,它也可以工作。但是我在这里的要求是专门循环遍历键列表并对它们进行一些处理,而不是使用硬编码键名检索键值,这就是 GetSection 或 GetValue
  • 为什么?你确定你没有误解配置系统,尝试使用与使用 ASP.NET 的配置相同的方式?
  • 顺便说一句,您可以使用AsEnumerable() 枚举所有对

标签: asp.net asp.net-core asp.net-web-api asp.net-core-webapi


【解决方案1】:

这样的东西可以让您专门迭代 appsettings 文件中的键和值。

public static void DoSomethingWithConfigurationManager(IConfiguration config)
{
    var root = config as ConfigurationRoot;
    if (root != null)
    {
        var appSettingsProvider = root.Providers.FirstOrDefault(x => x is JsonConfigurationProvider && ((JsonConfigurationProvider)x).Source?.Path == "appsettings.json") as JsonConfigurationProvider;
        if (appSettingsProvider != null)
        {
            var dataElement = appSettingsProvider.GetType().GetProperty("Data", BindingFlags.Instance | BindingFlags.NonPublic);
            var dataValue = (SortedDictionary<string, string>)dataElement.GetValue(appSettingsProvider);
            foreach (var item in dataValue)
            {
                //Do something with item.Key and item.Value
            }
        }
    }

【讨论】:

    【解决方案2】:

    我在我的program.cs 文件中使用了这样的方法,它使用appsettings.json 文件正确获取了值,你也可以这样尝试。

    Program.cs

    IHost host = new HostBuilder()            
                     .ConfigureAppConfiguration((hostContext, configApp) =>
                     {
                         configApp.AddJsonFile($"appsettings.json", true);
                         configApp.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true);
    }
    

    appsettings.json

    {
      "https_port": 443,
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*"
    }
    

    【讨论】:

    • 这不是 OP 要求的
    猜你喜欢
    • 2018-04-27
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2017-07-27
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多