Every time I call Configuration.GetSection
, the Value
property of the returned object is always null.
My Startup
constructor
appsettings.json
and go to Properties.Try add ConfigurationProvider to ConfigurationBuilder.
App configuration is provided from:
Just modify your ConfigureServices
method to be like following:
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<SqliteSettings>(Configuration.GetSection("SqliteSettings"));
services.AddMvc();
}
and it should work.
According to the Microsoft Docs: "When GetSection returns a matching section, Value isn't populated. A Key and Path are returned when the section exists."
If you want to see the values of that section you will need to call the GetChildren() method: Configuration.GetSection("SqliteSettings").GetChildren();
Or you can use:
Configuration.GetSection("SqliteSettings").Get<SqliteSettings>()
. The JSON does not need to have the same amount of properties to match. Unmatched nullable properties will be set to null and non-nullable unmatched properties will be set to their default value (e.g. int will be set to 0).
This worked for me in a console application:
var connectionString = config["ConnectionStrings:DefaultConnection"]
If you have an appsettings.Development.json
file, make sure the "ConnectionStrings:DefaultConnection" settings are the same in both the appsettings.Development.json
and appsettings.json
files.