从 ASP.NET 中的 config.json 中检索部分 5
Retrieve sections from config.json in ASP.NET 5
假设我有这样一个 config.json
:
{
"CustomSection": {
"A": 1,
"B": 2
}
}
我知道我可以使用 IConfiguration
对象来获取特定设置,即 configuration.Get("CustomSection:A")
,但我可以获取整个层次结构吗(在任何类型中 - 即使是原始字符串也可以) ?当我尝试 configuration.Get("CustomSection")
时,我得到了 null
结果,所以我认为这在默认情况下不受支持。
我的用例是一次获取整个配置字典,而不必获取每个单独的设置 - 某些属性在编译时可能未知。
configuration.Get是为了得到一个值来得到你需要的部分
IConfiguration mysection = configuration.GetConfigurationSection("SectionKey");
编辑:为 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;
}
您可以使用 ConfigurationBinder
并阅读所有内容 Dictionary<string, string>
以下是您可以用作示例的一些测试用例:https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Framework.Configuration.Binder.Test/ConfigurationCollectionBindingTests.cs
我能够像这样使用未知键加载和绑定多个子部分(自您的 post 以来语法略有变化;我建议密切关注 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 ;
});
我已经解决了一个类似的问题,我想将整个 IConfigurationRoot 或 IConfigurationSection 绑定到一个字典。这是一个扩展 class:
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);
详细解释见
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;
}
假设我有这样一个 config.json
:
{
"CustomSection": {
"A": 1,
"B": 2
}
}
我知道我可以使用 IConfiguration
对象来获取特定设置,即 configuration.Get("CustomSection:A")
,但我可以获取整个层次结构吗(在任何类型中 - 即使是原始字符串也可以) ?当我尝试 configuration.Get("CustomSection")
时,我得到了 null
结果,所以我认为这在默认情况下不受支持。
我的用例是一次获取整个配置字典,而不必获取每个单独的设置 - 某些属性在编译时可能未知。
configuration.Get是为了得到一个值来得到你需要的部分
IConfiguration mysection = configuration.GetConfigurationSection("SectionKey");
编辑:为 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;
}
您可以使用 ConfigurationBinder
并阅读所有内容 Dictionary<string, string>
以下是您可以用作示例的一些测试用例:https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Framework.Configuration.Binder.Test/ConfigurationCollectionBindingTests.cs
我能够像这样使用未知键加载和绑定多个子部分(自您的 post 以来语法略有变化;我建议密切关注 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 ;
});
我已经解决了一个类似的问题,我想将整个 IConfigurationRoot 或 IConfigurationSection 绑定到一个字典。这是一个扩展 class:
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);
详细解释见 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;
}