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

32,084

Solution 1

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.

Solution 2

You need to add the package below:

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

Solution 3

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)
});

Solution 4

An alternative solution I found from this blog post works as well. It has the added benefit of not needing to modify the Startup.cs file's Startup method signature.

In the buildOptions section add copyToOutput with the name of the file.

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true,
    "copyToOutput": "appsettings.json"
  },
  .... The rest of the file goes here ....

Solution 5

Right click appsettings.json -> Properties, then makes sure that Copy to Output Directory is set to "Copy Always"

Share:
32,084
Zhorian
Author by

Zhorian

Updated on June 20, 2020

Comments

  • Zhorian
    Zhorian almost 4 years

    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.

  • Sako73
    Sako73 over 7 years
    I was able to get it to work using .SetBasePath(Directory.GetCurrentDirectory()), as shown here: benfoster.io/blog/…
  • David
    David over 7 years
    Well, if it IS a hack, it's a hack the Microsoft does themselves when you set up a new blank ASP.NET Core Web Application.
  • Tadej
    Tadej over 6 years
    This was what i was missing. Thanks!
  • Beytan Kurt
    Beytan Kurt almost 6 years
    If you don't have project.json; don't worry. It is dumped, more detail on here
  • w0ns88
    w0ns88 over 5 years
    Thank you! builder.SetBasePath(Directory.GetCurrentDirectory()); fixed it for me.