.NET · Backendbeginner
.NET appsettings.json
An ASP.NET Core appsettings.json with logging levels, connection strings, AllowedHosts, Kestrel endpoints and a strongly typed options section. It is the base layer of .NET configuration.
dotnetaspnetcoreappsettingsconfigurationcsharp
Preview
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "example.com;*.example.com",
"ConnectionStrings": {
"Default": "Host=localhost;Port=5432;Database=app;Username=app"
},
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://0.0.0.0:8080"
}
}
},
"Smtp": {
"Host": "smtp.example.com",
"Port": 587,
"UseStartTls": true,
"From": "[email protected]"
},
"FeatureFlags": {
"NewCheckout": false
}
}AI actions
Documentation
Purpose
Hold an ASP.NET Core app's non-secret settings, which the configuration system layers with environment-specific files, environment variables and secret stores.
When to use
Any ASP.NET Core or .NET worker app. Put defaults here, overrides in appsettings.{Environment}.json, and secrets in user-secrets, environment variables or Key Vault.
Required fields
- None: the file is optional, but Logging and AllowedHosts are in every template
Optional fields
- Logging.LogLevel: minimum levels per category ("Default", "Microsoft.AspNetCore")
- ConnectionStrings: read with GetConnectionString("Name")
- AllowedHosts: host filtering, e.g. "example.com;*.example.com"
- Kestrel.Endpoints: listen URLs and certificates
- Custom sections: bound to a class with services.Configure<T>(...) or AddOptions<T>().Bind(...)
Best practices
- Later sources win: appsettings.json < appsettings.{Environment}.json < user secrets (Development) < environment variables < command line.
- Override nested keys from environment variables with a double underscore: Smtp__Port=2525.
- Bind sections to options classes with ValidateOnStart() so bad config fails at boot.
Security considerations
- Do not commit real passwords in ConnectionStrings; use user-secrets locally and Key Vault or environment variables in production.
- Avoid AllowedHosts "*" in production behind a public endpoint.
- Keep Logging at Information or above in production; Debug can log request data.