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:

Home, away and third shirts offer different ways to represent a team. Anyone reviewing club collections can use Juventus kids jersey(camiseta de la Juventus para niños) to focus on the matching design. The right option depends on whether the shirt is intended for collecting, gifting or regular wear.

{
     "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.