appsettings.json is where an ASP.NET Core app keeps its settings. It is only the first layer, though. Understanding the layering is most of the job.
The layering order
Each source overrides the ones before it:
appsettings.jsonappsettings.{Environment}.json, such asappsettings.Development.json- User secrets (Development environment only)
- Environment variables
- Command-line arguments
So defaults go in appsettings.json, per-environment tweaks go in the environment file, and secrets come from 3–5 or a vault. Secrets never go in the committed file.
The usual sections
{
"Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } },
"AllowedHosts": "example.com;*.example.com",
"ConnectionStrings": { "Default": "Host=localhost;Database=app;Username=app" }
}
- Logging.LogLevel: minimum level per category. Use the most specific category name you can.
- AllowedHosts: host-header filtering.
"*"allows any host. - ConnectionStrings: read with
builder.Configuration.GetConnectionString("Default").
Environment variables for nested keys
The : path separator becomes __:
export ConnectionStrings__Default="Host=db;Database=app;Username=app;Password=..."
export Smtp__Port=2525
This works the same way in Docker, Kubernetes and Azure App Service settings.
Typed options instead of string lookups
"Smtp": { "Host": "smtp.example.com", "Port": 587, "UseStartTls": true }
builder.Services.AddOptions<SmtpOptions>()
.Bind(builder.Configuration.GetSection("Smtp"))
.ValidateDataAnnotations()
.ValidateOnStart();
ValidateOnStart() makes a missing or invalid value fail at boot, not on the first email.
Kestrel endpoints
"Kestrel": { "Endpoints": { "Http": { "Url": "http://0.0.0.0:8080" } } }
Containers typically listen on 8080 over plain HTTP and let the ingress terminate TLS.
Security notes
- Locally, run
dotnet user-secrets set "ConnectionStrings:Default" "...". It stores values outside the repo. - In production, use environment variables, Key Vault references or a managed identity, never a committed password.
- Avoid
Debuglog levels in production. They can log request data.
Open the appsettings template and ask the workspace to "add a Redis connection string" or "lower Microsoft.AspNetCore logging to Warning", then review the diff.