Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration is empty

I'm trying to read config data from appsettings.json, which looks like this:

{
    Owner: {
        Name: "Dave",
        City: "Dusseldorf"
    }
}

In Startup.cs I have:

public Startup(IConfiguration configuration)
{
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json");
    Configuration = builder.Build();
}

In my class I have

private readonly IConfiguration _configuration;

public MyClass(
    IConfiguration configuration
    )
{
    _configuration = configuration;
}

...and later in the same class

string name= _configuration["Owner.Name"];

But this is always empty. However, if I add a breakpoint on that line and expand the _configuration class I can see a list of Providers (Count = 5) including one for appsettings.json, and if I expand this I can see my setting values, including Name.

However these don't seem to be accessible from code.

Am I using the wrong class/namespace? How should I read these values?

like image 845
CompanyDroneFromSector7G Avatar asked Nov 20 '25 02:11

CompanyDroneFromSector7G


1 Answers

One of the worst reasons this can happen is that your appsettings.json is not built (copied into the bin folder). Try right-clicking the file -> select properties and set to Copy if newer or Copy Always. That would make sure the file is properly copied and found by the configuration logic.

like image 93
GeorgiG Avatar answered Nov 22 '25 15:11

GeorgiG