Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources:
- Settings files, such as appsettings.json
- Environment variables
- Azure Key Vault
- Azure App ConfigurationCommand-line arguments
- Custom providers, installed or created
- Directory files
- In-memory .NET objects
Configuration sources are read in the order that their configuration providers are specified. Order configuration providers in code to suit the priorities for the underlying configuration sources that the app requires.
A typical sequence of configuration providers is:
- appsettings.json
- appsettings.{Environment}.json
- User secrets
- Environment variables using the Environment Variables configuration provider.
- Command-line arguments using the Command-line configuration provider.
A common practice is to add the Command-line configuration provider last in a series of providers to allow command-line arguments to override configuration set by the other providers.
Application and Host Configuration
ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management. The ASP.NET Core templates create a WebApplicationBuilder which contains the host. While some configuration can be done in both the host and the application configuration providers, generally, only configuration that is necessary for the host should be done in host configuration.
WebApplication.CreateBuilder initializes a new instance of the WebApplicationBuilder class with preconfigured defaults. The initialized WebApplicationBuilder
(builder
) provides default configuration for the app in the following order, from highest to lowest priority:
- Command-line arguments using the Command-line configuration provider.
- Non-prefixed environment variables using the Non-prefixed environment variables configuration provider.
- User secrets when the app runs in the
Development
environment. appsettings.{Environment}.json
using the JSON configuration provider. For example,appsettings.Production.json
andappsettings.Development.json
.- appsettings.json using the JSON configuration provider.
- A fallback to the host configuration.
Default host configuration sources
The following list contains the default host configuration sources from highest to lowest priority:
- Command-line arguments using the Command-line configuration provider
ASPNETCORE_
-prefixed environment variables using the Environment variables configuration provider.DOTNET_
-prefixed environment variables using the Environment variables configuration provider.
When a configuration value is set in host and application configuration, the application configuration is used.