ASP.NET Core configuration precedence with launchSettings, user secrets and appsettings.* across environments
0 reputation · 03 Apr 2023, 22:48 UTC
0 reputation · 03 Apr 2023, 22:48 UTC
ASP.NET Core configuration is layered across launchSettings.json, user secrets, appsettings.json and environment-specific files such as appsettings.Development.json. Visual Studio debug profiles commonly set ASPNETCORE_ENVIRONMENT and rely on development-only sources, while production relies on published files and host environment variables.
The precise goal is to define a consistent source of truth for each setting class and to document the intended winner when the same key exists in multiple providers. launchSettings.json and user secrets are development conveniences and are not deployed. appsettings.json is normally published, while environment-specific JSON files are loaded only when the environment name matches.
Provider ordering can vary by ASP.NET Core generation and hosting model, creating uncertainty about precedence when a key is present in both an environment variable and an environment-specific JSON file. Teams need a clear decision on ownership rather than relying on implicit override behavior.
Which provider order should be adopted for production to ensure environment variables override appsettings.Production.json? Should appsettings.Development.json ever be published, or should all non-default values be moved to environment configuration? How to document the intended source of truth for each setting class without relying on local debug behavior?
Use the default provider order; environment variables already win over appsettings.Production.json in production. No re‑ordering is required, and appsettings.Development.json should not be published.
appsettings.json → appsettings.{Environment}.json → User Secrets (only when ASPNETCORE_ENVIRONMENT=Development) → Environment variables → Command‑line arguments.launchSettings.json is not a provider; it only sets environment variables for dotnet run, so those variables participate in the Environment variables step and therefore override any JSON file values.ASPNETCORE_ENVIRONMENT (e.g., appsettings.Development.json in production) are never loaded and are excluded from the publish output by default.If you observe that a JSON value is winning over an environment variable, the most common cause is a custom ConfigurationBuilder ordering in Program.cs (or Startup.cs) that places JSON providers after environment variables, inverting the default precedence.
Add* calls that reorder providers (e.g., calling AddJsonFile after AddEnvironmentVariables).dotnet publish -c Release) and confirm that appsettings.Development.json and the user‑secrets folder are absent from the output.appsettings.json, can be overridden by environment variables, or is development‑only (user secrets).Are you adding configuration providers in a custom order in Program.cs? If yes, the default precedence is overridden and you will need to adjust the order to place environment variables after the JSON files.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.