Sep112015

Access All IConfiguration Properties in MVC 6

Just a quick snippet I thought I'd share: I needed to access all of the configuration settings available in my app's IConfiguration object. I created an extension method that maps it to an IDictionary.

public static IDictionary<string, object> ExpandToDictionary(this IConfiguration configuration)
{
    var dictionary = new Dictionary<string, object>();
    var configurationSections = configuration.GetChildren();
    if(configurationSections.Any())
    {
        foreach(var section in configurationSections)
        {
            var subConfigurationSections = section.GetChildren();
            if(subConfigurationSections.Any())
            {
                var serializedSection = section.ExpandToDictionary();
                dictionary.Add(section.Key, serializedSection);
            }
            else
            {
                var flattenedValue = configuration[section.Key];
                dictionary.Add(section.Key, flattenedValue);
            }
        }
    }
 
    return dictionary;
}

I can then iterate through this by key to see all values, or even serialize it to JSON if I want to use the configuration in my view's JavaScript.