Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
ASP.NET Core Basics: Pipeline, Services and Settings  ›  Capstone Project

App Settings and Configuration

App Settings and Configs 15 minASP.NET Core merges settings from JSON files, environment-specific files, secrets, environment variables and the command line, with later sources overriding earlier ones.

App Settings and Configuration

On your laptop ReadingRoom uses a local SQLite file and lets members borrow five books. In production it talks to a real database and allows three. The code must be the same in both places; only the settings differ. ASP.NET Core's configuration system is how one build behaves correctly everywhere.

The rule

Settings stack in layers; the last layer to set a key wins.

WebApplication.CreateBuilder reads these sources, in this order:

  1. appsettings.json
  2. appsettings.{Environment}.json — e.g. appsettings.Development.json
  3. User secrets — only when the environment is Development
  4. Environment variables
  5. Command-line arguments

The environment name comes from the ASPNETCORE_ENVIRONMENT variable (Development, Staging or Production; Production if unset).

{
  "ConnectionStrings": { "Library": "Data Source=library.db" },
  "Loans": { "MaxPerMember": 5, "LengthDays": 14 }
}

Keys are paths joined by colons: Loans:MaxPerMember. Environment variables cannot contain colons on every system, so they use a double underscore: Loans__MaxPerMember=3.

Rather than reading strings by key everywhere, bind a section to a class — the options pattern:

public class LoanOptions
{
    public int MaxPerMember { get; set; }
    public int LengthDays { get; set; }
}

builder.Services.Configure<LoanOptions>(builder.Configuration.GetSection("Loans"));

public class LoanService(IOptions<LoanOptions> options)
{
    private readonly int _max = options.Value.MaxPerMember;
}

Secrets never go in committed JSON. During development, dotnet user-secrets set "ConnectionStrings:Library" "..." stores them outside the project folder; in production, use environment variables or a secret store.

The shortcut

To predict a value, start at the bottom of the list and go up: the first source that sets the key is the answer.

What is MaxPerMember in production?Step 1 of 4
  1. Command line?

    The app was started with no arguments. Keep going up.

The trap: committing a real connection string with a password to appsettings.json. Git remembers it even after you delete it.

Drills

Answer fast — trust the rule.

quick drill

appsettings.json says 5 and the command line passes --Loans:MaxPerMember=7. Which value is used?

quick drill

How is Loans:MaxPerMember written as an environment variable?

quick drill

When are user secrets loaded by default?

quick drill

Which line binds the Loans section to LoanOptions for injection?

Remember

  • Settings stack in layers; the last layer to set a key wins.
  • JSON → environment JSON → user secrets → environment variables → command line.
  • Bind sections to classes with the options pattern; keep secrets out of Git.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainWhich value wins?

ReadingRoom's appsettings.json sets Loans:MaxPerMember to 5, appsettings.Production.json sets it to 3, and the production server has the environment variable Loans__MaxPerMember=4. Explain which value the app uses in production and why, where the database password should live, and how LoanService should read these settings.