How does configuration work in ASP.NET Core (appsettings.json and environment variables)?

IntermediateConcept.NET builds (dotnet CLI)
Answer

Short answer

ASP.NET Core loads configuration in layers, and later layers override earlier ones: appsettings.json → appsettings.{Environment}.json → user secrets (dev only) → environment variables → command-line arguments. The environment is chosen by ASPNETCORE_ENVIRONMENT (Development, Staging, Production).

Example

// appsettings.json
{ "ConnectionStrings": { "Default": "Host=localhost;Database=shop" },
  "Features": { "NewCheckout": false } }
// appsettings.Production.json
{ "Logging": { "LogLevel": { "Default": "Warning" } } }
export ASPNETCORE_ENVIRONMENT=Production
export ConnectionStrings__Default="Host=db.prod;Database=shop;Username=app;Password=***"
export Features__NewCheckout=true

Double underscore __ separates nesting levels in env vars (a colon works on Windows but not in Linux env var names).

In Kubernetes

env:
  - name: ASPNETCORE_ENVIRONMENT
    value: Production
  - name: ConnectionStrings__Default
    valueFrom: { secretKeyRef: { name: shop-db, key: connection-string } }

What to say in the interview

"Configuration is layered: appsettings.json, then the environment-specific file picked by ASPNETCORE_ENVIRONMENT, then environment variables, then command line. We keep secrets out of the JSON files and inject them as env vars like ConnectionStrings__Default from Kubernetes Secrets or Key Vault."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: .NET apps: dotnet build & publish

More on .NET builds (dotnet CLI)