Roadmap › Modern .NET — Hosting, Configuration, DI & Deployment › Module: Hosting, Configuration, DI and Logging
Lessons in this course (9)
4. Configuration and the Options pattern
Basic★ Must knowLesson 4 of 9
Sources, later overrides earlier: appsettings.json → appsettings.{ASPNETCORE_ENVIRONMENT}.json → User Secrets (Development) → environment variables → command-line args. Also Azure Key Vault and Azure App Configuration providers.
json
{
"ConnectionStrings": { "Db": "Server=.;Database=Shop;Trusted_Connection=True;TrustServerCertificate=True" },
"Smtp": { "Host": "smtp.example.com", "Port": 587 }
}csharp
var host = builder.Configuration["Smtp:Host"]; // ':' separates sections
// env var override: Smtp__Host=smtp.prod.example.com ('__' in environment variables)
builder.Services.AddOptions<SmtpOptions>().BindConfiguration("Smtp").ValidateDataAnnotations().ValidateOnStart();bash
dotnet user-secrets init
dotnet user-secrets set "Smtp:Password" "local-only-secret"