Zhorian
Zhorian

Reputation: 802

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

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.

Upvotes: 32

Views: 34063

Answers (9)

bhatt ravii
bhatt ravii

Reputation: 382

Actually for this you need to provide root path from your environment variable so you need to pass IHostingEnvironment reference to provide root path:

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

and if you can't find AddJsonFile method then you have to add using Microsoft.Extensions.Configuration.Json;

Upvotes: 8

user3413723
user3413723

Reputation: 12263

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

Upvotes: 7

SchattenJager
SchattenJager

Reputation: 333

In .NET Core 2.0, you would update the .csproj file to copy the JSON file to the output directory so that it can be accessed, like so:

<ItemGroup>
    <Folder Include="wwwroot\" />
    <None Include="appsettings.json" CopyToOutputDirectory="Always" />
</ItemGroup>

Upvotes: 5

Groppe
Groppe

Reputation: 3879

The answers that suggest adding .SetBasePath(env.ContentRootPath) in Startup.cs depend upon a prior step.

First, add the line .UseContentRoot(Directory.GetCurrentDirectory()) to your WebHostBuilder construction in Program.cs like so:

public class Program
{
    public static void Main(string[] args)
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddEnvironmentVariables()
            .Build();

        var host = new WebHostBuilder()
            .UseConfiguration(config)
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

Then the following will work:

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

    Configuration = builder.Build();
}

Upvotes: 1

Chris F Carroll
Chris F Carroll

Reputation: 12380

If you have done anything with Project Debug Properties, then you may have inadvertently overwritten the starting directory:

Project -> Right-click -> Properties -> Debug -> Profile and then look at the entry in Working Directory.

The simplest is that it be blank.

Upvotes: 0

T&#226;n
T&#226;n

Reputation: 1

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

Upvotes: 16

user6655970
user6655970

Reputation: 229

You need to add the package below:

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

Upvotes: 22

Dzejms
Dzejms

Reputation: 3258

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 ....

Upvotes: 9

Zhorian
Zhorian

Reputation: 802

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.

Upvotes: 26

Related Questions