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

Zhorian picture Zhorian · Jul 9, 2016 · Viewed 29.3k times · Source

So I've finally got round to looking at Core and I've fallen at the first hurdle. I'm following the Pluralsight ASP.NET Core Fundamentals course and I'm getting an exception when trying too add the appsettings.json file to the configuration builder.

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

    Configuration = builder.Build();
}

The error I'm getting is The configuration file 'appsettings.json' was not found and is not optional. But I have created the directly under my solution just like in the course video.

Answer

Zhorian picture Zhorian · Jul 9, 2016
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.