Configuration.GetSection always returns null
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).
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.
- Right-click on
appsettings.json
and go to Properties. - Select Copy to output directory = Copy always.