Oluwafemi Oluwafemi - 2 months ago 27
ASP.NET (C#) Question

How to read AppSettings values from Config.json in ASP.NET vNext

I have got my AppSettings setup in Config.json like this:

{
"AppSettings": {
"token": "1234"
}
}


I have searched online on how to read AppSettings values from config.json but I could not get anything useful.

I tried:

var configuration = new Configuration();
var appSettings = configuration.Get("AppSettings"); // null
var token = configuration.Get("token"); // null


I know with ASP.NET 4.0 you can do this:

System.Configuration.ConfigurationManager.AppSettings["token"];


But how do I do this in ASP.NET vNext?

Answer

This has had a few twists and turns. I've modified this answer to be up to date with ASP.NET Core as of 15/09/2016.

This is mostly taken from the official documentation:

To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. Then, use the Options pattern to access individual settings. Let's say we have an appsettings.json file that looks like this:

{
  "ApplicationName": "MyApp",
  "Version": "1.0.0"
}

And we have a POCO object representing the configuration:

public class MyConfig
{
    public string ApplicationName { get; set; }
    public int Version { get; set; }
}

Now we build the configuration in in Startup.cs:

public class Startup 
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

        Configuration = builder.Build();
    }
}

If we want to inject our configuration to our controllers, we'll need to register it with the runtime. We do so via Startup.ConfigureServices:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    // Add functionality to inject IOptions<T>
    services.AddOptions();

    // Add our Config object so it can be injected
    services.Configure<MyConfig>(Configuration);
}

And we inject it like this:

public class HomeController : Controller
{
    private readonly IOptions<MyConfig> config;

    public HomeController(IOptions<MyConfig> optionsAccessor)
    {
        this.config = config;
    }

    // GET: /<controller>/
    public IActionResult Index() => View(config.Value);
}

Full Startup class:

public class Startup 
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

        Configuration = builder.Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        // Add functionality to inject IOptions<T>
        services.AddOptions();

        // Add our Config object so it can be injected
        services.Configure<MyConfig>(Configuration);
    }
}