
How appsettings.Production.json Really Works: Configuration Layers, Not Replacements
- كاتب موثّق
- .NET 10
محتويات المقال
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
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.
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.
What actually happens
Every source becomes a flat list of keys
: 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:
appsettings.jsonappsettings.{Environment}.json, for exampleappsettings.Production.jsonUser secrets, only in the Development environment Environment variables, with __standing in for:, as inEmail__HostCommand-line arguments
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
string[] from every index it finds.
Nothing can delete a key
"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
{ "Cors": { "AllowedOrigins": [ "http://localhost:3000", "http://localhost:5173" ] }, "Email": { "Host": "localhost", "Port": 25 }}{ "Cors": { "AllowedOrigins": [ "https://app.example.com" ] }, "Email": { "Host": "smtp.example.com" }}Cors:AllowedOrigins:0 | https://app.example.com | appsettings.Production.json |
Cors:AllowedOrigins:1 | http://localhost:5173 | appsettings.json |
Email:Host | smtp.example.com | appsettings.Production.json |
Email:Port | 25 | appsettings.json |
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
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:
{ "Email": { "Host": "localhost", "Port": 25 }}{ "Cors": { "AllowedOrigins": [ "http://localhost:3000", "http://localhost:5173" ] }}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
Audit the base file for lists Search appsettings.jsonfor arrays. For each one, ask whether every environment should get all of its items. If not, move it to the environment files.Diff the effective configuration, not the files Run the app once per environment with the debug view and compare the output. That's what the app sees; the files are only inputs. Use one layer per kind of value Keep shared defaults in appsettings.json, environment differences inappsettings.{Environment}.json, and secrets in user secrets locally and in environment variables or a vault in production.Override arrays with every index When an environment variable or a later file must change an array, set all of its indexes, such as Cors__AllowedOrigins__0andCors__AllowedOrigins__1, and make sure the lower layer has no extra ones.
Key takeaways
appsettings.{Environment}.jsonis layered on top ofappsettings.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().
التعليقات (0)