How does configuration work in ASP.NET Core?

ASP.NET Core Configuration Basic 1–2 Yrs2–3 Yrs Conceptual 1 min read

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):

  1. appsettings.json
  2. appsettings.{Environment}.json (Development / Staging / Production)
  3. User Secrets (Development only)
  4. Environment variables
  5. 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 inject IOptions<SmtpOptions>.

Never put secrets in appsettings.json in source control — use user secrets locally and environment variables / a key vault in production.