ASP.NET Core MVC Intermediate

Configuration and appsettings.json

Settings such as connection strings, API endpoints and page sizes should not be hard-coded. ASP.NET Core reads configuration from several sources and merges them, so the same app can use different settings on your laptop and in production. This lesson explains the sources, their order, and how to keep secrets out of source control.

Configuration sources: later ones win
  1. Command-line argumentsHighest priority, such as --Shop:PageSize=50
  2. Environment variablesUse __ for sections: Shop__PageSize=50
  3. User secretsDevelopment only, stored outside the project folder
  4. appsettings.{Environment}.jsonFor example appsettings.Production.json
  5. appsettings.jsonBase settings for every environment

When two sources define the same key, the source higher in this list overrides the lower one.

The idea in plain words

WebApplication.CreateBuilder loads configuration from all of these sources and merges them into one IConfiguration. Keys are hierarchical, written with a colon in code, such as Shop:PageSize. Because later sources override earlier ones, you keep defaults in appsettings.json and override them per environment or per server without changing code.

Example: settings and reading them

C#
// appsettings.json
{
  "Shop": { "PageSize": 20, "SupportEmail": "[email protected]" },
  "ConnectionStrings": { "DefaultConnection": "Server=...;Database=ShopDb;..." }
}

// Reading values directly
var pageSize = builder.Configuration.GetValue<int>("Shop:PageSize");
var conn = builder.Configuration.GetConnectionString("DefaultConnection");

// Better: bind a section to a class (the options pattern)
builder.Services.Configure<ShopSettings>(builder.Configuration.GetSection("Shop"));

Example: keep secrets out of your code

User secrets are for development. In production, use environment variables or a secret store such as Azure Key Vault.

Shell
dotnet user-secrets init
dotnet user-secrets set "Smtp:Password" "my-local-password"

# Production server or container
export ConnectionStrings__DefaultConnection="Server=prod-db;Database=ShopDb;..."

Common mistakes

  • Committing passwords and API keys in appsettings.json.
  • Using a single colon in environment variable names on Linux; use a double underscore.
  • Expecting user secrets to work in production; they are loaded only in the Development environment.
  • Misspelling a key, which silently returns null or the default value.

Interview question

How does ASP.NET Core decide which value to use when a key appears in several configuration sources?

Configuration providers are added in order, and when the same key exists in more than one provider, the value from the provider added last wins. By default the order is appsettings.json, appsettings.{Environment}.json, user secrets (in Development), environment variables, then command-line arguments, so command-line arguments override everything else.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.