问题
How can I check if a specific section in loaded ASP.NET Core configuration file exist?
I have a JSON configuration file that I load it in Startup
class via ConfigurationBuilder.AddJsonFile
method.
This JSON file is an array with this layout:
{
"Url": "",
"Regex": [ "", "" ],
"Keys": {
"Title": "",
"Description": "",
"Keywords": [ "" ]
}
}
But some of them doesn't have Keys
. I tried to check return type of section.GetSection("Keys")
against null
, But it doesn't return null
even if Keys
section isn't present.
回答1:
Use GetChildren
method:
var keysExists = Configuration.GetChildren().Any(x => x.Key == "Keys"));
回答2:
Also it is possible to use use Exists extension method from Microsoft.Extensions.Configuration.Abstractions. Exapmle:
var section = Configuration.GetSection("Keys")
var sectionExists = section.Exists();
or
public bool IsExistsConfigurationSection(string sectionKey)
{
return Configuration.GetSection(sectionKey).Exists();
}
回答3:
I would use ConfigurationExtensions.Exists(IConfigurationSection)
method as Алексей Сидоров suggested because this method as it's said in the official documentation
determines whether the section has a Value or has children
Not only children.
Here are my extension methods with checking section existence which might be helpful for someone.
Usage:
_configRoot.GetSectionChecked(nameof(Site));
// instead of
// IConfigurationSection section = _configRoot.GetSection(nameof(Site));
// if (!section.Exists())
// throw new ArgumentException(nameof(Site));
services.ConfigureSection<Site>(_configRoot);
// instead of
// services.Configure<Site>("Site", _config);
services.ConfigureSection<PostsConfig>(_configRoot.GetSection(nameof(Site)), nameof(Site.Posts));
// instead of
// services.Configure<PostsConfig>(_configRoot.GetSection(nameof(Site)).GetSection(nameof(Site.Posts)));
Extension methods:
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
public static class ConfigurationExtensions
{
public static IConfigurationSection GetSectionChecked(this IConfiguration config, string sectionName)
{
var section = config.GetSection(sectionName);
CheckSection(section);
return section;
}
public static IServiceCollection ConfigureSection<TOptions>(this IServiceCollection services, IConfiguration config, string sectionName = null)
where TOptions : class
{
string typeName = sectionName ?? typeof(TOptions).Name;
IConfigurationSection section = config.GetSectionChecked(typeName);
return services.Configure<TOptions>(section);
}
public static IServiceCollection ConfigureSection<TOptions>(this IServiceCollection services, IConfigurationSection section, string sectionName = null)
where TOptions : class
{
CheckSection(section);
return services.ConfigureSection<TOptions>((IConfiguration)section, sectionName);
}
private static void CheckSection(IConfigurationSection section)
{
if (!section.Exists())
throw new ArgumentException($"Configuration section '{section.Path}' doesn't exist.");
}
}
来源:https://stackoverflow.com/questions/61779244/how-to-know-if-a-key-exists-on-iconfiguration