我在我的 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
在下面的屏幕截图中可以看到并没有包含任何内容:
![enter image description here](https://istack.dev59.com/FeSt9.webp)
要让它工作,您还需要做什么?
我尝试了var v = this._configuration.GetSection("Section1").GetSection("Key1");
,其中_configuration
是IConfiguration
类型,并且结果符合预期。但是,正如我之前提到的那样,我不想这样做,相反,我想遍历所有appsettings的键列表并对它们执行某些操作。
任何帮助都将是伟大的。