skip to content

In ASP.NET Core, how does an environment variable like ConnectionStrings__Adoptions override appsettings.json, and what do the ASPNETCORE_ and DOTNET_ prefixes change?

level: middleimportance: must knowfreq 70%

answer

  1. unprefixed variables, high in the chain
  2. double underscore becomes a colon
  3. prefix stripped, but loaded first
  4. CUSTOMCONNSTR_ maps to ConnectionStrings
  5. read once at startup

basics

~20 s

WebApplication.CreateBuilder loads every environment variable after the JSON files and user secrets, turning __ into the : separator, so ConnectionStrings__Adoptions overrides ConnectionStrings:Adoptions. ASPNETCORE_ and DOTNET_ variables are also loaded with the prefix stripped, but at the bottom of the chain.

solid answer

~40 s

The default builder adds an environment-variables provider with no prefix after `appsettings.json`, `appsettings.{Environment}.json` and user secrets, and before command-line arguments. Because `:` is not a portable character in variable names, the provider replaces `__` with `:`, so `ConnectionStrings__Adoptions` becomes `ConnectionStrings:Adoptions` and beats the file value; that is how a container receives its database connection. Variables starting with `ASPNETCORE_` and `DOTNET_` are also read by prefixed providers that strip the prefix, but those are added first, below the JSON files: they exist to feed host settings such as the environment name and URLs. So `ASPNETCORE_ConnectionStrings__Adoptions` loses to a value in `appsettings.json`. The provider also maps the connection-string prefixes some hosting platforms inject, such as `CUSTOMCONNSTR_Adoptions`, into `ConnectionStrings:Adoptions`. Variables are read once; changing one needs a restart.

code

bash · 5 lines
bash
export ConnectionStrings__Adoptions="Host=db;Database=adoptions;Username=app"
export Features__OnlineApplications=false
# lowest precedence: appsettings.json still wins for this key
export ASPNETCORE_Features__VideoMeetAndGreet=false
dotnet AdoptionsApi.dll

go deeper

for a junior

Remember that ConnectionStrings__Adoptions sets ConnectionStrings:Adoptions and that an environment variable beats appsettings files.

for a middle

Explain the double-underscore normalisation, where the unprefixed provider sits, and why the prefixed providers exist and lose to the files.

for a senior

Use this when a container ignores a value: check for a prefixed name, a missing index, or a stale process that never restarted after the variable changed.

for a principal

Agree a naming convention for platform-supplied settings so operators never reach for ASPNETCORE_ on app keys and overrides stay discoverable.

## Where environment variables sit `WebApplication.CreateBuilder(args)` builds one `ConfigurationManager` and adds sources in this order (later wins): 1. Environment variables starting with `ASPNETCORE_`, prefix removed. 2. Environment variables starting with `DOTNET_`, prefix removed. 3. `appsettings.json`, then `appsettings.{Environment}.json` (and the `{ApplicationName}.settings*.json` pair). 4. User secrets, in Development only. 5. **All environment variables, no prefix filter.** 6. Command-line arguments, when `args` is passed (they are also added once early, for host settings). Step 5 is the provider people mean by "an environment variable overrides appsettings". It sits above every file and below the command line. ## The double underscore Hierarchical keys use `:` (`ConnectionStrings:Adoptions`, `Features:OnlineApplications`), but a colon is not accepted in variable names by many shells and platforms. The provider therefore **normalises `__` to `:`** before storing the key: | Variable | Configuration key | |---|---| | `ConnectionStrings__Adoptions` | `ConnectionStrings:Adoptions` | | `Features__OnlineApplications` | `Features:OnlineApplications` | | `Shelters__0` | `Shelters:0` | Key lookups ignore case, so `CONNECTIONSTRINGS__ADOPTIONS` works too, although the variable name itself is case-sensitive on Linux when you set or read it outside .NET. ```yaml services: adoptions-api: image: adoptions-api:1.4 environment: ASPNETCORE_ENVIRONMENT: Production ConnectionStrings__Adoptions: "Host=db;Database=adoptions;Username=app" Features__VideoMeetAndGreet: "false" ``` Inside the container `builder.Configuration.GetConnectionString("Adoptions")` returns the compose value, whatever the image's `appsettings.json` contains. ## What the prefixes change The prefixed providers have a different job. They are added **before** the JSON files so that host-level settings are known early: the environment name (`ASPNETCORE_ENVIRONMENT` becomes `environment`), the URLs, the content root. Two consequences follow: - **The prefix is stripped**, so `ASPNETCORE_Features__OnlineApplications` is visible as `Features:OnlineApplications`. - **It is at the bottom of the chain**, so `appsettings.json` defining the same key beats it. Putting an app setting behind `ASPNETCORE_` is a common reason an override "does nothing". Use unprefixed names for application settings and keep `ASPNETCORE_`/`DOTNET_` for host settings. (The unprefixed provider also loads `ASPNETCORE_ENVIRONMENT`, but under its full name, which nothing reads.) ## Connection-string prefixes The unprefixed provider recognises a naming scheme that some hosting platforms use to inject connection strings, and rewrites it: - `CUSTOMCONNSTR_Adoptions` becomes `ConnectionStrings:Adoptions`. - `SQLCONNSTR_Adoptions` becomes `ConnectionStrings:Adoptions` plus `ConnectionStrings:Adoptions_ProviderName = System.Data.SqlClient`. - `POSTGRESQLCONNSTR_` and `MYSQLCONNSTR_` behave the same way with their provider names. You rarely set these by hand; knowing them explains a connection string that appears from nowhere. ## Values are always strings A variable carries text, and the configuration system stores it as text. `Features__OnlineApplications=false` becomes the string `"false"`, which `GetValue<bool>` or options binding converts later; `False` and `FALSE` convert too, while `0` or `no` fail to convert to `bool`. Where the platform allows an empty variable, `Features__OnlineApplications=` defines the key with an empty string, which is different from leaving it unset: the key no longer falls through to the file, and a `bool` conversion of the empty string fails. ## Custom prefixes `builder.Configuration.AddEnvironmentVariables(prefix: "ADOPT_")` registers another provider that keeps only variables starting with `ADOPT_` and removes the prefix, so `ADOPT_Features__OnlineApplications` becomes `Features:OnlineApplications`. Because it is added after `CreateBuilder`, it sits above everything else. Teams use this to keep their own settings apart from whatever else the platform injects. ## Lifetime of a variable The provider enumerates the process environment **once**, when it loads. Unlike the JSON files it has no change token, so editing a variable in a running container does nothing; the process must restart. ## Interview checklist - `__` becomes `:`; keys are case-insensitive. - Unprefixed variables beat the JSON files and user secrets and lose to the command line. - `ASPNETCORE_`/`DOTNET_` variables are loaded first, with the prefix stripped, for host settings. - Variables are read once at startup.

  • Why does ASPNETCORE_Features__OnlineApplications=false not switch the feature off when appsettings.json sets it to true?
    `WebApplicationBuilder` adds the `ASPNETCORE_` provider as its very first source so host settings are known early. Its keys lose the prefix, but every later source, including `appsettings.json`, overrides them. Use the unprefixed `Features__OnlineApplications`, which is loaded after the files.
  • How do you override an array element through environment variables?
    Use the index as a path segment: `Shelters__0=central` sets `Shelters:0`. Elements you do not set fall through from the files, so overriding a shorter list leaves the old trailing elements in place.
  • Does changing an environment variable in a running ASP.NET Core process change configuration?
    No. The environment-variables provider reads the process environment once when it loads and has no change token. Only file-based sources registered with reloadOnChange refresh while running; a new variable value needs a restart.

The settings files are a station's printed timetable; an unprefixed environment variable is a platform-change notice pinned over one line, and it wins for that train. An ASPNETCORE_ variable is a notice filed behind the timetable: it exists, but the printed line covers it.

  • Environment variables must use a colon, like ConnectionStrings:Adoptions, to reach nested keys.
  • Prefixing a variable with ASPNETCORE_ gives it the highest precedence.
  • Environment variables are re-read on every IConfiguration access, so changes apply live.
  • Only variables with the ASPNETCORE_ prefix are visible to IConfiguration.
  • appsettings.Production.json beats environment variables because it is environment-specific.