Extract sections from config.json in ASP.NET 5

Say I have config.json :

 { "CustomSection": { "A": 1, "B": 2 } } 

I know that I can use the IConfiguration object to get certain parameters, i.e. configuration.Get("CustomSection:A") , but can I grab the entire hierarchy (in any type - even with the original string would be fine)? When I try configuration.Get("CustomSection") , I get a null result, so I think this is not supported by default.

My use case is to capture entire configuration dictionaries at once without having to capture every single parameter - some properties may not be known at compile time.

+3
c # asp.net-core
source share
6 answers

Edit: Update this answer for kernel version 1.0.

This is possible now if you use a strongly typed object, for example:

 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; } 
+1
source share

configuration.Get to get the value to get the desired section

 IConfiguration mysection = configuration.GetConfigurationSection("SectionKey"); 
+2
source share

I solved a similar problem where I wanted to associate the entire IConfigurationRoot or IConfigurationSection with the Dictionary. Here is the extension 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; } } } 

And using the extension:

 IConfigurationRoot config; var data = config.ToDictionary(); var data = config.GetSection("CustomSection").ToDictionary(); 

There is an optional parameter (stripSectionPath) to either save the full path of the section key or cut out the section path, leaving the relative path.

 var data = config.GetSection("CustomSection").ToDictionary(false); 
+2
source share

I managed to download and associate several subnets with such unknown keys (the syntax has changed slightly since publication, I recommend following the github project and their unit tests to see how it works at the moment):

 var objectSections = Configuration.GetSection("CustomObjects").GetChildren(); var objects = objectSections.ToDictionary(x => x.Key, x => { var obj = new CustomObject(); ConfigurationBinder.Bind(x, obj); return obj ; }); 
+1
source share

You can use ConfigurationBinder and read everything as Dictionary<string, string>

Here are some sample tests you can use as an example: https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Framework.Configuration.Binder.Test/ConfigurationCollectionBindingTests.cs

0
source share

For a detailed explanation, see https://dotnetcodr.com/2017/01/20/introduction-to-asp-net-core-part-3-the-configuration-file/

Below is an example from the site:

The configuration file has the following:

 "Languages": { ".NET": [ "C#", "VB.NET", "F#" ], "JVM": ["Java", "Scala", "Clojure"] } 

Download this configuration as follows:

 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; } 
0
source share

All Articles