我们说我有config.json
这样的话:
{
"CustomSection": {
"A": 1,
"B": 2
}
}
我知道我可以使用IConfiguration
对象获取特定设置,即configuration.Get("CustomSection:A")
,但是我可以抓住整个层次结构(任何类型 - 甚至原始字符串都可以)?当我尝试configuration.Get("CustomSection")
时,我得到null
结果,因此我认为默认情况下不支持此功能。
我的用例是立即抓取整个配置字典而不必抓取每个单独的设置 - 在编译时可能不知道某些属性。
答案 0 :(得分:3)
我已经解决了类似的问题,我希望将整个IConfigurationRoot或IConfigurationSection绑定到Dictionary。这是一个扩展类:
public class ConfigurationExtensions
{
public static Dictionary<string, string> ToDictionary(this IConfiguration config, bool stripSectionPath = true)
{
var data = new Dictionary<string, string>();
var section = stripSectionPath ? config as IConfigurationSection : null;
ConvertToDictionary(config, data, section);
return data;
}
static void ConvertToDictionary(IConfiguration config, Dictionary<string, string> data = null, IConfigurationSection top = null)
{
if (data == null) data = new Dictionary<string, string>();
var children = config.GetChildren();
foreach (var child in children)
{
if (child.Value == null)
{
ConvertToDictionary(config.GetSection(child.Key), data);
continue;
}
var key = top != null ? child.Path.Substring(top.Path.Length + 1) : child.Path;
data[key] = child.Value;
}
}
}
使用扩展程序:
IConfigurationRoot config;
var data = config.ToDictionary();
var data = config.GetSection("CustomSection").ToDictionary();
有一个可选参数(stripSectionPath)可以保留完整的部分键路径,也可以去掉部分路径,留下相对路径。
var data = config.GetSection("CustomSection").ToDictionary(false);
答案 1 :(得分:2)
configuration.Get用于获取值以获取所需的部分
IConfiguration mysection = configuration.GetConfigurationSection("SectionKey");
答案 2 :(得分:1)
编辑:更新Core的1.0版本的答案。
如果您使用强类型对象,现在可以这样做,例如:
public class CustomSection
{
public int A {get;set;}
public int B {get;set;}
}
//In Startup.cs
services.Configure<CustomSection>(Configuration.GetSection("CustomSection"));
//You can then inject an IOptions instance
public HomeController(IOptions<CustomSection> options)
{
var settings = options.Value;
}
答案 3 :(得分:1)
我能够使用这样的未知密钥加载和绑定多个子部分(自发布以来语法略有改变;我建议关注github项目及其单元测试以了解它当前是如何工作的):
var objectSections = Configuration.GetSection("CustomObjects").GetChildren();
var objects = objectSections.ToDictionary(x => x.Key, x =>
{
var obj = new CustomObject();
ConfigurationBinder.Bind(x, obj);
return obj ;
});
答案 4 :(得分:0)
您可以使用ConfigurationBinder
并将所有内容读作Dictionary<string, string>
以下是一些您可以使用的测试用例:https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Framework.Configuration.Binder.Test/ConfigurationCollectionBindingTests.cs
答案 5 :(得分:0)
有关详细说明,请参阅 https://dotnetcodr.com/2017/01/20/introduction-to-asp-net-core-part-3-the-configuration-file/
以下是该网站的示例:
配置文件包含:
"Languages": {
".NET": [ "C#", "VB.NET", "F#" ],
"JVM": ["Java", "Scala", "Clojure"]
}
按如下方式加载此配置:
IConfigurationSection languagesSection = configRoot.GetSection("Languages");
IEnumerable<IConfigurationSection> languagesSectionMembers = languagesSection.GetChildren();
Dictionary<string, List<string>> platformLanguages = new Dictionary<string, List<string>>();
foreach (var platform in languagesSectionMembers)
{
List<string> langs = (from p in platform.GetChildren() select p.Value).ToList();
platformLanguages[platform.Key] = langs;
}