How does configuration work in ASP.NET Core?
Quick Answer
Configuration is built from an ordered set of providers — appsettings.json, appsettings.{Environment}.json, user secrets (dev), environment variables, command-line args — merged into one key/value store exposed as IConfiguration. Later providers override earlier ones, so environment variables beat the JSON file.
Configuration in ASP.NET Core is layered. The host stacks providers in order — appsettings.json, then the environment-specific file, then user secrets in development, then environment variables, then command-line args — and merges them into one IConfiguration. Later providers win, which is how an environment variable overrides the JSON in production without changing the file. I read values directly with _config.GetConnectionString, or bind a section to a strongly typed options class. Secrets never go in appsettings.json in the repo — user secrets locally, environment variables or a vault in production.
The default host adds providers in this order (each overriding the last):
appsettings.jsonappsettings.{Environment}.json(Development / Staging / Production)- User Secrets (Development only)
- Environment variables
- Command-line arguments
Access it three ways:
- Directly:
_config["ConnectionStrings:Default"]or_config.GetConnectionString("Default"). - Bound to a class:
_config.GetSection("Smtp").Get<SmtpOptions>(). - The options pattern:
services.Configure<SmtpOptions>(config.GetSection("Smtp"))then injectIOptions<SmtpOptions>.
Never put secrets in appsettings.json in source control — use user secrets locally and environment variables / a key vault in production.