skipnothing/.NET Platform

The Configuration System

Set `ConnectionStrings:Default` in appsettings.json, then export that same setting as an environment variable. The exported value silently wins, even though you never reopened the JSON file and never told anything which source to prefer.

  • A nested JSON key and a __ env var resolve to one colon key; values are strings.
  • Stack position sets priority, not source type: an env var beats committed JSON only by being added later.
  • Save appsettings.json and a running app sees it; change an env var and nothing moves until restart.
BUILDS ON
01

One Flat Dictionary of Strings

An appsettings.json looks like a tree:

{
  "ConnectionStrings": { "Default": "Host=db;Port=5432" },
  "Logging": { "LogLevel": { "Default": "Information" } },
  "FeatureEnabled": true
}

But the system that reads it keeps no tree. It flattens every leaf into one long list of string-to-string pairs, joining the nesting with a colon:

ConnectionStrings:Default = Host=db;Port=5432
Logging:LogLevel:Default  = Information
FeatureEnabled            = True

That is the whole store: a flat dictionary where the path through the JSON becomes a single string key. There is no nested object in memory, only "Logging:LogLevel:Default" pointing at a string. Notice FeatureEnabled: the JSON boolean true arrives as the string "True". Every value is text, the typing happens later, when you read it back out. You reach a value by its full colon path, and IConfiguration is the service that holds the dictionary and answers those lookups.

JSON TREE COLLAPSES TO FLAT KEYS

Keep going, sign up to unlock the rest

4 more parts in this topic, plus 25+ more topics in .NET Platform.

Sign up, it's freeSee the full .NET Platform
Configuration & Options0/5#17 DI Anti-Patterns
#19 The Options Pattern