Explaining the ASP.NET 5 Configuration Framework

ASP.NET 5 (aka vNext) has a new configuration system which is designed to be lightweight and to work everywhere. This means no more web.config XML hell! Hooray! (However you can use XML files if you want…)

Loading Settings

The Configuration class is defined in the Microsoft.Framework.ConfigurationModel namespace. This is included as a dependency if you reference the Microsoft.AspNet.Mvc package so there’s no need to explicitly reference the nuget package.

Currently, there are 3 different file types that are supported: Json, Xml and Ini. You can also pull in settings from Environment Variables (like those you’d set in Azure). The Ini load method is in the default package, however the Json and Xml load methods are each defined in the Microsoft.Framework.ConfigurationModel.Json and Microsoft.Framework.ConfigurationModel.Xml packages, respectively. This is because the Json and Xml loaders have different dependencies, and the AspNet team wants to ensure you only load and reference what you need.

To load a settings file, you instantiate a new Configuration and use one of the Add... methods:

var config = new Configuration();
config.AddJsonFile("path/to/file.json");
config.AddXmlFile("path/to/file.xml");
config.AddIniFile("path/to/file.ini");

Getting Settings

To get settings out, call the Get method. For example, if you have a Json config like so:

{
     "window": {
         "background": {
             "color": "blue"
         }
     }
}

You can get the color out with the full “namespace” (for lack of a better term) of the key:

var backgroundColor = config.Get("window:background:color");

The team looks like they’re hitting their goal of a lightweight configuration system that’s easy to use. They still have some kinks to work out (like how are arrays going to be handled, if at all?) but the initial impressions are positive.