الانتقال إلى المحتوى
.NETASP.NET Core· 6 دقائق للقراءة

How appsettings.Production.json Really Works: Configuration Layers, Not Replacements

Every configuration source is flattened into keys and stacked on the ones before it. Why a shorter array in production keeps the base file's extra entries, and how to see which file set each key.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • .NET 10
محتويات المقال

A security scan of a production API reports that it accepts cross-origin requests from http://localhost:5173. The team opens appsettings.Production.json: it lists exactly one allowed origin, https://app.example.com, and three people approved it. The extra origin comes from appsettings.json, the file nobody checked, because "production doesn't use that one".

The common misconception

The belief: appsettings.Production.json is the production version of appsettings.json. When the app runs in Production, ASP.NET Core loads it instead of the base file, so whatever you don't copy into it doesn't exist in production.

It's an easy model to fall into. The file name reads like "the production copy". The project template's appsettings.Development.json repeats the base file's Logging section almost line for line, which looks like a full copy with a few keys removed. And for simple values, "replaced" and "overridden" look identical: set Email:Host in the production file and production uses that host either way.

The difference only shows up for the keys the environment file doesn't mention. Under replacement they'd vanish; in reality they stay. The wrong model also breeds a defensive habit: copying the whole base file into every environment file "to be safe". It protects nothing, because the keys still merge, and it leaves two copies of each setting to drift apart.

What actually happens

Every source becomes a flat list of keys

A reads one source and turns it into key-value pairs, with : joining the levels. { "Email": { "Port": 25 } } becomes the key Email:Port with the value "25". Arrays get no special treatment: each item becomes a key ending in its index, so a list of two origins becomes Cors:AllowedOrigins:0 and Cors:AllowedOrigins:1.

Sources are stacked, and the last one wins per key

WebApplication.CreateBuilder adds these sources, from lowest to highest priority:

  1. appsettings.json
  2. appsettings.{Environment}.json, for example appsettings.Production.json
  3. User secrets, only in the Development environment
  4. Environment variables, with __ standing in for :, as in Email__Host
  5. Command-line arguments

When you read a key, the value comes from the highest source that has it. Each key is decided on its own: the production file overriding Email:Host says nothing about Email:Port, which keeps its value from the base file. The environment name comes from DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT, and when neither is set, it's Production. Both JSON files are also watched by default (reloadOnChange is true), so saving one on a running server changes the configuration; whether your code sees the new value depends on how it reads it.

Arrays merge by index

Because array items are just keys, a shorter array in a higher layer overrides only the indexes it has. Microsoft's docs show the same effect: three modules in one source, two in the next, and the result keeps the third module from the first source. The binder then builds a string[] from every index it finds.

Nothing can delete a key

A higher layer can change a value, but it can't remove a key that a lower layer set. On .NET 10, an empty array, "AllowedOrigins": [], in the environment file only sets Cors:AllowedOrigins itself to an empty string; the indexed keys from the base file are still there, and the bound array still has their entries.

The CORS list, layer by layer

Here are the two files from the hook. The base file was written for local development; the production file was meant to replace its list:

JSON appsettings.json
{  "Cors": {    "AllowedOrigins": [ "http://localhost:3000", "http://localhost:5173" ]  },  "Email": { "Host": "localhost", "Port": 25 }}

Run the app as Production and ask the configuration where each value came from. For these keys, it reports:

KeyFinal valueSet by
Cors:AllowedOrigins:0https://app.example.comappsettings.Production.json
Cors:AllowedOrigins:1http://localhost:5173appsettings.json
Email:Hostsmtp.example.comappsettings.Production.json
Email:Port25appsettings.json

The Email rows are what everyone expects: the host changes and the port keeps its default. The Cors rows follow exactly the same rule. Index 0 was overridden, index 1 was never mentioned, so the bound list has two origins, and one of them is a developer's machine.

Fixing it: base values are defaults, not a draft

Treat appsettings.json as the defaults every environment shares, and keep anything that differs per environment, lists above all, out of it. Each environment file then supplies the whole list, with nothing underneath to leak through. The production file doesn't change:

JSON appsettings.json
{  "Email": { "Host": "localhost", "Port": 25 }}

To check the result instead of trusting it, print the configuration's debug view. It lists every key, its final value and the provider that set it:

C# Program.cs
var builder = WebApplication.CreateBuilder(args);// every key, its final value and the provider that set it// local debugging only: the output includes secretsConsole.WriteLine(builder.Configuration.GetDebugView());var app = builder.Build();app.Run();

How to apply it in your project

  1. Audit the base file for listsSearch appsettings.json for arrays. For each one, ask whether every environment should get all of its items. If not, move it to the environment files.
  2. Diff the effective configuration, not the filesRun the app once per environment with the debug view and compare the output. That's what the app sees; the files are only inputs.
  3. Use one layer per kind of valueKeep shared defaults in appsettings.json, environment differences in appsettings.{Environment}.json, and secrets in user secrets locally and in environment variables or a vault in production.
  4. Override arrays with every indexWhen an environment variable or a later file must change an array, set all of its indexes, such as Cors__AllowedOrigins__0 and Cors__AllowedOrigins__1, and make sure the lower layer has no extra ones.

Key takeaways

  • appsettings.{Environment}.json is layered on top of appsettings.json, key by key; it never replaces it.
  • The order is base file, environment file, user secrets in Development, environment variables, then the command line, and the last source that sets a key wins.
  • Arrays are flattened to indexed keys, so a shorter or empty array in a higher layer keeps the extra items from below.
  • Keep per-environment lists out of the base file, and check the effective configuration with GetDebugView().

📚 المصادر

  1. Configuration in ASP.NET Core — Microsoft Learn
  2. ASP.NET Core runtime environments — Microsoft Learn
  3. Configuration in .NET — Microsoft Learn

التعليقات (0)