【问题标题】:Load a custom appsettings file based on prior appsettings files根据之前的 appsettings 文件加载自定义 appsettings 文件
【发布时间】:2020-11-19 13:12:12
【问题描述】:

我正在尝试为该应用程序所涉及的集成环境构建许多 appsettings 文件。 根据 appsettings..json 中的内容,我想加载另一个 appsettings 文件。

我正在program.cs 中尝试类似于以下内容:

IConfiguration tempConfiguration = config.Build();
string[] items = tempConfiguration.GetValue<string[]>("Application:EnvironmentPointers");
foreach (string environment in items)
{ 
    config.AddJsonFile($"integratedApplication.{environment}.json", optional: false, reloadOnChange: true);
}

这会在 GetValue() 部分引发空引用异常。

appsettings..json 类似于以下内容:

{
  "Application": {
    "EnvironmentPointers": [ "Development", "Staging" ]
  }
}

有什么想法可以实现吗?

【问题讨论】:

    标签: c# asp.net-core asp.net-core-3.1


    【解决方案1】:

    考虑改为以下方法

    //...
    
    string[] items = tempConfiguration.GetSection("Application:EnvironmentPointers").Get<string[]>();
    
    //...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-12
      • 2011-11-01
      • 2012-07-31
      • 2019-07-23
      • 2022-08-22
      • 2012-07-06
      • 1970-01-01
      相关资源
      最近更新 更多