C# 如何检查MVC核心配置文件中是否存在节?
如何检查加载的ASP.NET核心配置文件中是否存在特定节 我有一个JSON配置文件,通过C# 如何检查MVC核心配置文件中是否存在节?,c#,asp.net-mvc,asp.net-core,asp.net-core-mvc,C#,Asp.net Mvc,Asp.net Core,Asp.net Core Mvc,如何检查加载的ASP.NET核心配置文件中是否存在特定节 我有一个JSON配置文件,通过ConfigurationBuilder.AddJsonFile方法将其加载到Startup类中 此JSON文件是具有以下布局的数组: { "Url": "", "Regex": [ "", "" ], "Keys": { "Title": "", "Description": "", "Keywords": [ "" ] } } 但是其中一些没有键。我
ConfigurationBuilder.AddJsonFile
方法将其加载到Startup
类中
此JSON文件是具有以下布局的数组:
{
"Url": "",
"Regex": [ "", "" ],
"Keys": {
"Title": "",
"Description": "",
"Keywords": [ "" ]
}
}
但是其中一些没有
键
。我试图对照null
检查section.GetSection(“Keys”)
的返回类型,但即使Keys
节不存在,它也不会返回null
。使用GetChildren
方法:
var keysExists = Configuration.GetChildren().Any(x => x.Key == "Keys"));
也可以使用Microsoft.Extensions.Configuration.Abstracts中的Exists扩展方法。示例:
var section = Configuration.GetSection("Keys")
var sectionExists = section.Exists();
或
我将使用
ConfigurationExtensions.Exists(IConfigurationSection)
方法,因为该方法如中所述
确定节是有值还是有子项
不仅仅是孩子
下面是我的扩展方法,用于检查节的存在性,这可能会对某些人有所帮助
用法:
_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)));
\u configRoot.GetSectionChecked(站点名称));
//而不是
//IConfigurationSection=\u configRoot.GetSection(站点名称);
//如果(!section.Exists())
//抛出新的ArgumentException(nameof(Site));
services.ConfigureSection(\u configRoot);
//而不是
//services.Configure(“站点”,配置);
services.ConfigureSection(_configRoot.GetSection(nameof(Site)),nameof(Site.Posts));
//而不是
//services.Configure(_configRoot.GetSection(nameof(Site)).GetSection(nameof(Site.Posts));
扩展方法:
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.");
}
}
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用制度;
公共静态类配置扩展
{
公共静态IConfigurationSection GetSectionChecked(此IConfiguration配置,字符串sectionName)
{
var section=config.GetSection(sectionName);
检查段(段);
返回段;
}
公共静态IServiceCollection ConfigureSection(此IServiceCollection服务,IConfiguration配置,string sectionName=null)
地点:班级
{
字符串类型名称=节名称??类型(TopOptions).Name;
IConfigurationSection=config.GetSectionChecked(typeName);
返回服务。配置(部分);
}
公共静态IServiceCollection配置节(此IServiceCollection服务,IConfigurationSection节,string sectionName=null)
地点:班级
{
检查段(段);
return services.ConfigureSection((IConfiguration)section,sectionName);
}
专用静态无效检查部分(IConfigurationSection)
{
如果(!section.Exists())
抛出新ArgumentException($“配置节“{section.Path}”不存在。”);
}
}
您是否尝试了配置.GetSection(“键”)
?@Sanket我迭代数组并将每个元素读入节
变量。我尝试了节.GetSection(“键”)
,但它返回一个对象,其内部属性或节为null
而非自身。
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.");
}
}