ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile("appsettings.json"); not finding file

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json");

    Configuration = builder.Build();
}

This seems to do the trick. However unsure this is the proper way to do it. Kinda feels like a hack.


You need to add the package below:

    "Microsoft.Extensions.Configuration.Json": "1.0.0"

Another way:

appsettings.json:

{
    "greeting": "A configurable hello, to you!"
}

Startup.cs:

using Microsoft.Extensions.Configuration; // for using IConfiguration
using System.IO; // for using Directory

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

    public Startup()
    {
        var builder = new ConfigurationBuilder();
        builder.SetBasePath(Directory.GetCurrentDirectory());
        builder.AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }    
}

In the Configure method:

app.Run(async (context) =>
{
    // Don't use:
    // string greeting = Configuration["greeting"]; // null

    string greeting = Configuration.GetSection("greeting").Value;
    await context.Response.WriteAsync(greeting)
});