MRB*_*MRB 8 c# asp.net-mvc asp.net-core-mvc asp.net-core
如何检查加载的ASP.NET Core配置文件中是否存在特定部分?
我有一个JSON配置文件,我Startup通过ConfigurationBuilder.AddJsonFile方法在类中加载它.
此JSON文件是具有此布局的数组:
{
"Url": "",
"Regex": [ "", "" ],
"Keys": {
"Title": "",
"Description": "",
"Keywords": [ "" ]
}
}
Run Code Online (Sandbox Code Playgroud)
但其中一些没有Keys.我试图检查section.GetSection("Keys")反对的返回类型null,但null即使Keys段不存在也不返回.
使用GetChildren方法:
var keysExists = Configuration.GetChildren().Any(x => x.Key == "Keys"));
Run Code Online (Sandbox Code Playgroud)
也可以使用 Microsoft.Extensions.Configuration.Abstractions 中的 Exists 扩展方法。例子:
var section = Configuration.GetSection("Keys")
var sectionExists = section.Exists();
Run Code Online (Sandbox Code Playgroud)
或者
public bool IsExistsConfigurationSection(string sectionKey)
{
return Configuration.GetSection(sectionKey).Exists();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1840 次 |
| 最近记录: |