Advanced usage
Folder conventions, source precedence and binding configuration to POCOs.
Folder conventions
Conventions used by the loader:
.envfiles underEnvironments/.env.<EnvironmentName>, fallback toEnvironments/.env.local.appsettingsJSON underSettings/appsettings.<EnvironmentName>.json, fallback toSettings/appsettings.local.json.
File names are matched without regard to case, so .env.Development and .env.development — or
appsettings.Local.json and appsettings.local.json — are equally acceptable on every platform. Matching
on exact case only resolved on Windows and silently found nothing on a case-sensitive file system.
Precedence
When building IConfiguration, sources are added in the order you call them on the same
IConfigurationBuilder. JSON files added later override earlier ones.
Binding to POCOs
Binding works through Microsoft.Extensions.Configuration:
public sealed class AppSettings
{
public string? Name { get; set; }
public ConnectionStrings ConnectionStrings { get; set; } = new();
}
public sealed class ConnectionStrings
{
public string? Default { get; set; }
}
var configuration = new ConfigurationBuilder()
// if you have additional files, add them here; LoadAppSettings adds the environment-specific file
.Build();
// After LoadAppSettings on the builder
var settings = configuration.Get<AppSettings>();