Read Config file in ASP.NET 5



ASP.NET vNext has a new configuration system.

With the new version of ASP.NET more than one file type is supported.

Currently, there are 3 different file types that are supported: Json, Xml and Ini.

For example, we can define connection string in config.json file:

{
    "Data": {
        "DefaultConnection": { 
            "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true"
        }
    }
}

You can also get  settings from Environment Variables.

To read connection string from config.json from any location in your app you need to add a reference to Microsoft.Framework.ConfigurationModel.Json.

Now simple import namespace and use code below to read connection string from config.json:

 

using Microsoft.Framework.ConfigurationModel.Json;

var configuration = new Configuration()
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();

var connectionString = configuration.Get("Data:DefaultConnection:ConnectionString");

 

You can also load configuration from multiple different config files:

 

  var configuration = new Configuration()
                              .AddJsonFile("config.json")
                              .AddIniFile("config.ini")
                              .AddXmlFile("config.xml")
                              .AddEnvironmentVariables();

To use xml files you need to reference Microsoft.Framework.ConfigurationModel.Xml and for Ini Microsoft.Framework.ConfigurationModel.

Supporting multiple config files and reading values from config files has never been easier.

If you like this article don’t forget to subscribe to this blog and make sure you don’t miss new upcoming blog posts.

 

  • Cesar Bludata

    Hello, I’m trying to use this, but I’m with some problem, I hope that you can help me 🙂

    First, the namespace that I neet to put is “using Microsoft.Framework.ConfigurationModel;” without the “.Json” in the end. I tried with “.Json” but the method “Configuration” wasn’t found.
    In my project.json file, I’ve both libs, “Microsoft.Framework.ConfigurationModel” and “Microsoft.Framework.ConfigurationModel.Json”.

    After that “diference”, when the code is executed, I got an exception “Object reference not set to an instance of an object.” and the stack trace is:

    at Microsoft.Framework.ConfigurationModel.PathResolver.get_ApplicationBaseDirectory()
    at Microsoft.Framework.ConfigurationModel.JsonConfigurationExtension.AddJsonFile(IConfigurationSourceRoot configuration, String path, Boolean optional)
    at Microsoft.Framework.ConfigurationModel.JsonConfigurationExtension.AddJsonFile(IConfigurationSourceRoot configuration, String path)
    at Project.SharedKernel.MyConfigurations..cctor() in C:ProjectProject.SharedKernelMyConfigurations.cs:line 86

    My class:

    static CredinetConfigurations()
    {
    var configuration = new Configuration().AddJsonFile(“MyConfigurations.json”).AddEnvironmentVariables();