7.2 — appsettings.json and Environment Overrides¶
← Roadmap · Section: 7. Configuration and Options Prev: 7.1 The configuration system · Next: 7.3 Environment variables and User Secrets
Markers: 🔥 · Confidence: 🔴 · Last reviewed: —
TL;DR¶
appsettings.json holds your application's settings. appsettings.{Environment}.json is
layered on top of it, overriding only the values that differ.
- The environment file is chosen by
ASPNETCORE_ENVIRONMENT, soProductionloadsappsettings.Production.json. - Put only the differences in the environment file. Copying the whole base file causes drift.
- ⚠️ These files are in source control. No real secrets go in either of them.
Definitions¶
| Term | What it is | What it means |
|---|---|---|
appsettings.json |
a JSON file | The base settings file. Always loaded. Shared defaults. |
appsettings.{Env}.json |
a JSON file | Loaded on top of the base file for that environment only. |
| Layering | the merge behaviour | The environment file's values overwrite the base file's for matching keys. |
ASPNETCORE_ENVIRONMENT |
an environment variable | Selects which environment file loads. See 4.7. |
optional |
a parameter on AddJsonFile |
Whether a missing file is an error. Base file false, environment file true. |
reloadOnChange |
a parameter on AddJsonFile |
Re-read the file when it changes on disk. See 7.9. |
ConnectionStrings |
a conventional section | Read with GetConnectionString("Name"), which is shorthand for ["ConnectionStrings:Name"]. |
| Copy to output | a build setting | These files are copied to the publish folder. That is why they deploy. |
$schema |
a JSON property | Points at a schema so your editor offers IntelliSense and validation. |
The concept¶
Two files, one result¶
Every ASP.NET Core project starts with two:
appsettings.json ← always loaded
appsettings.Development.json ← loaded only when the environment is Development
They are merged, not chosen between. The base file supplies everything, and the environment file overrides individual keys.
// appsettings.json — the shared baseline
{
"Logging": {
"LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" }
},
"ConnectionStrings": { "Default": "Server=localhost;Database=App;Trusted_Connection=true" },
"Orders": { "MaxItems": 100, "AutoApproveUnder": 50.00 }
}
// appsettings.Development.json — ONLY what differs
{
"Logging": {
"LogLevel": { "Default": "Debug" }
}
}
Running in Development, the application sees:
Logging:LogLevel:Default=Debug— overriddenLogging:LogLevel:Microsoft.AspNetCore=Warning— inherited from the baseConnectionStrings:Default= localhost — inheritedOrders:MaxItems= 100 — inherited
Note that the override is per key, not per section. Supplying Logging:LogLevel:Default did
not remove the sibling Microsoft.AspNetCore entry. That per-key merging is the behaviour to
internalise.
A picture: a base recipe with a sticky note on top. The note says "use butter instead of oil". Everything else on the recipe still applies — you did not have to rewrite it.
⚠️ Do not copy the whole base file¶
The most common mistake:
// appsettings.Production.json — ✗ the entire base file, copied and edited
{
"Logging": {
"LogLevel": { "Default": "Warning", "Microsoft.AspNetCore": "Warning" }
},
"ConnectionStrings": { "Default": "Server=prod;..." },
"Orders": { "MaxItems": 100, "AutoApproveUnder": 50.00 }
}
Now Orders:MaxItems exists in four files. Changing it means changing all four, someone forgets
one, and environments drift apart in ways nobody notices until behaviour differs.
Override only what actually changes. If a value is the same everywhere, it belongs in the base file alone.
How it works¶
The simple version — the file set¶
appsettings.json ← base, always
appsettings.Development.json ← local machines
appsettings.Staging.json ← staging
appsettings.Production.json ← production
The environment name comes from ASPNETCORE_ENVIRONMENT, and the file name must match it
exactly (4.7).
⚠️ Case sensitivity is a real deployment trap. Environment name checks in code are
case-insensitive, but file names on Linux are not. So appsettings.production.json is
silently ignored when the environment is Production — the app starts, uses only the base file,
and connects to the wrong database. On Windows it works, which is why this survives testing.
Match the case exactly.
The ConnectionStrings convention¶
{
"ConnectionStrings": {
"Default": "Server=localhost;Database=App",
"ReadReplica": "Server=replica;Database=App"
}
}
// These are identical
var cs = builder.Configuration.GetConnectionString("Default");
var cs = builder.Configuration["ConnectionStrings:Default"];
GetConnectionString is just shorthand. The section name is a convention rather than anything
special — but following it means the shorthand works and other developers find it where they
expect.
How it actually works — the array merge trap ⚠️¶
Because merging happens key by key, arrays behave surprisingly.
Flattened, that is:
AllowedOrigins:0 = "https://a.com"
AllowedOrigins:1 = "https://b.com"
AllowedOrigins:2 = "https://c.com"
Now override it:
// appsettings.Production.json — trying to allow only one origin
{ "AllowedOrigins": [ "https://prod.com" ] }
That sets AllowedOrigins:0 and nothing else. The result is:
AllowedOrigins:0 = "https://prod.com" ← overridden
AllowedOrigins:1 = "https://b.com" ← STILL THERE
AllowedOrigins:2 = "https://c.com" ← STILL THERE
You intended one allowed origin and got three. For a CORS policy that is a security problem (10.10).
Three ways round it:
// 1. Do not put lists in the base file if they will be overridden
// appsettings.json — no AllowedOrigins at all
// appsettings.Production.json
{ "AllowedOrigins": [ "https://prod.com" ] }
// 3. Clear it explicitly before binding
var origins = config.GetSection("AllowedOrigins").Get<string[]>() ?? [];
Option 1 is usually cleanest. If a list varies by environment, it should live only in the environment files.
The deeper detail — how these files reach production¶
appsettings*.json files are marked to copy to the output directory, so dotnet publish
includes them (3.8).
That has two consequences.
All of them ship, including appsettings.Development.json. Only the matching one is loaded,
so it does no harm functionally — but it is in the deployed artifact and readable by anyone with
access to the container image. Do not treat "it is only for development" as protection.
Changes require a redeploy, unless reloadOnChange is enabled and something updates the file
in place (7.9).
You can control the copy behaviour if needed:
<ItemGroup>
<Content Update="appsettings.Development.json" CopyToOutputDirectory="Never" />
</ItemGroup>
Editor support¶
Adding a schema reference gives you IntelliSense and validation in Visual Studio and VS Code:
{
"$schema": "https://json.schemastore.org/appsettings.json",
"Logging": { "LogLevel": { "Default": "Information" } }
}
Small, and it catches typos in framework setting names — which otherwise fail silently, because an unrecognised key is simply an unused key.
Code¶
A realistic pair of files:
// appsettings.json — defaults that are true everywhere
{
"$schema": "https://json.schemastore.org/appsettings.json",
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore.Database.Command": "Warning"
}
},
"AllowedHosts": "*",
"Orders": {
"MaxItemsPerOrder": 100,
"AutoApproveUnder": 50.00,
"ReservationMinutes": 15
},
"Payments": {
"TimeoutSeconds": 30,
"MaxRetries": 3
// NOTE: no ApiKey here — that is a secret. See 7.3 and 7.4.
}
}
// appsettings.Development.json — ONLY the differences
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.EntityFrameworkCore.Database.Command": "Information" // see the SQL locally
}
},
"ConnectionStrings": {
"Default": "Server=(localdb)\\mssqllocaldb;Database=AppDev;Trusted_Connection=true"
}
}
// appsettings.Production.json — also only the differences
{
"Logging": {
"LogLevel": { "Default": "Warning" }
},
"Orders": {
"ReservationMinutes": 5 // shorter hold under real load
}
// No connection string — that comes from an environment variable or Key Vault
}
Three deliberate decisions there:
The production connection string is absent from the file. It arrives as an environment variable or from Key Vault, so the real credential is never in source control (7.4).
EF Core command logging is raised in development only. At Information it logs every SQL
statement — invaluable locally, overwhelming in production
(5.14).
Each environment file is short. Everything not mentioned is inherited.
Interview questions¶
Q: How do appsettings.json and appsettings.{Environment}.json work together?
They are merged rather than chosen between. The base file is always loaded and supplies the
defaults; the environment file is loaded on top of it and overrides individual keys. The
environment is selected by ASPNETCORE_ENVIRONMENT, so a value of Production loads
appsettings.Production.json. The merge is per key, not per section, so overriding one log level
does not remove its siblings — which means each environment file should contain only the values
that actually differ.
Q: What goes wrong if you copy the whole base file into each environment file?
Every setting then exists in several places, so changing one means changing all of them. Someone inevitably misses one and the environments drift apart in ways nobody notices until behaviour differs between staging and production. Since the merge is per key, there is no need — anything not mentioned in the environment file is inherited automatically.
Q: What is the trap with arrays in configuration?
Arrays are flattened into indexed keys, and merging happens per key, so overriding an array does not replace it. Supplying a one-element array over a three-element one sets index zero and leaves indices one and two from the original in place. That is genuinely dangerous for something like a CORS allowed-origins list, where you intended to restrict it to one origin and silently kept three. The cleanest fix is to keep lists that vary by environment out of the base file altogether.
Q: Should connection strings go in appsettings.json?
A local development one is fine, since it is not a real credential. A production connection
string is not, because these files are in source control and anything in them is in the git
history permanently and visible to everyone with repository access. Production connection strings
belong in environment variables set by the hosting platform, or in a secret store like Key Vault.
Worth also knowing that all the appsettings files ship in the published output, including the
Development one, so "it is only for development" is not protection.
Traps ⚠️¶
- File names are case-sensitive on Linux.
appsettings.production.jsonis silently ignored when the environment isProduction— and works fine on Windows. - Arrays merge by index, so a shorter override leaves the original tail elements behind.
- Copying the whole base file into environment files causes configuration drift.
- No real secrets in any
appsettings*.json. They are in source control. - All the environment files ship in the published output, not just the active one.
- An unrecognised key is silently ignored. Typos in framework setting names do nothing, with
no warning. A
$schemareference catches many of them. - Comments are allowed in these files by the .NET JSON reader, despite not being standard JSON — but some tooling will complain.
Related¶
- 7.1 The configuration system — precedence and providers
- 7.3 Environment variables and User Secrets — where secrets go
- 7.4 Secret providers — Key Vault in production
- 7.9 Reload on change — picking up file edits
- 4.7 Environments — what selects the file
- 17.7 Secrets management — the security view
- 10.10 CORS — where the array trap bites