Getting started with ASP.NET 5 configuration

June 26, 2015 by Anuraj

.Net ASP.Net ASP.Net MVC HTML5 Visual Studio Windows Azure

When ASP.NET introduced, unlike conventional web.config, it was project.json. When I started exploring ASP.NET code, I couldn’t find the System.Configuration namespace as well. This post is about reading configuration in ASP.NET from project.json file. ASP.NET 5’s configuration system has been re-architected from previous versions of ASP.NET. The new configuration model provides streamlined access to key/value based settings that can be retrieved from a variety of sources.

Here is recommended approach for managing configuration in ASP.NET 5

  • As first step you need to include “Microsoft.Framework.ConfigurationModel” reference in project.json under dependencies.
  • Instantiate an instance of Configuration in your application’s Startup class. - Configuration class is just a collection of Sources, which provide the ability to read and write name/value pairs. After instantiating the configuration class, you need to set at least one configuration source to the configuration instance. You can use different configuration sources like JSON, XML, INI etc.
  • Use the Options pattern to access individual settings. - Options is a framework for accessing and configuring POCO settings. You will be creating a class which you can map to the configuration and you will be injecting this as a dependency to the controller using ASP.NET 5 dependency injection feature.

So here is the implementation. In Startup.cs, I have a Configuration property, and in the constructor I am creating an instance and assigning JSON file. You require reference of Microsoft.Framework.ConfigurationModel.

public Startup(IHostingEnvironment env)
{
    var configuration = new Configuration();
    configuration.AddJsonFile("config.json");
    
    Configuration = configuration;
}
public IConfiguration Configuration { get; set; }

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
}

After that I am reading the AppSettings configuration section. And in the controller class, you need to create a constructor which accepts IOptions argument.

private readonly AppSettings _appSettings;
public HomeController(IOptions<AppSettings> settingsAccessor)
{
    _appSettings = settingsAccessor.Options;
}

The ASP.NET runtime will inject the settings to controller constructor, from this you can start consuming the configuration values.

And here is the AppSettings class.

public class AppSettings
{
    public string SiteTitle { get; set; }
    public string InstrumentationKey { get; set; }
}

And here is the config.json file.

{
    "AppSettings": {
        "SiteTitle": "AppInsightsDemo",
        "InstrumentationKey": "db6da75a-s787-4a7b-gh88-b0041b8a9299"
    }
}

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub