How to properly read nested configuration values from config.json in ASP.NET5?

Peering deep into the bowels of the JsonConfigurationFileParser source with blame on for the enter/exit methods that look at:

private void VisitJObject(JObject jObject)
{
    foreach (var property in jObject.Properties())
    {
        EnterContext(property.Name);
        VisitProperty(property);
        ExitContext();
    }
}

private void EnterContext(string context)
{
    _context.Push(context);
    _currentPath = string.Join(":", _context.Reverse());
}

private void ExitContext()
{
    _context.Pop();
    _currentPath = string.Join(":", _context.Reverse());
}

it seems that the ASP.NET team should leave more illuminating check-in comments :).

My best guess is that there could be data stored in the config.json file that would need to have a . in it, whereas : would be less common. For instance:

"AppSettings": {
    "Site.Title": "Is .NET getting faster?"
},

It's a bad example, but it seems reasonable that they wanted to be as "safe" as possible and use something outside of the norm. If you wanted to store a type's full name, that would also be slightly easier without needing to worry about a stray period.

"AppSettings": {
    "ImportantTypeName": "WebApp.CoolStuff.Helpers.AwesomeClass"
},

using Microsoft.Extensions.Configuration;
using System.IO;

IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

var connectionString = configuration.GetValue<string>("ConnectionStrings:DefaultConnection");

// or

var connectionString2= configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value;  

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "myconnection"
  },
}

That's the convention that we decided upon when we first created the configuration model. We started with json in mind and : is the delimiter there.

Anyways, if you don't want to worry about those conventions, I recommend using the ConfigurationBinder which binds a configuration to a model (a strong type object). Here are the tests on GitHub that can serve as example.