ASP.NET Core Interview Question #45

How do environment variables map to nested configuration keys?

Application Structure & Configuration Junior Beginner

Quick Interview Answer

ASP.NET Core's environment variable configuration provider supports hierarchical configuration. Because a colon is not portable in environment variable names on every platform, a double underscore is commonly used to represent a colon.

Detailed Explanation

ASP.NET Core's environment variable configuration provider supports hierarchical configuration. Because a colon is not portable in environment variable names on every platform, a double underscore is commonly used to represent a colon.

For example, ConnectionStrings__DefaultConnection maps to ConnectionStrings:DefaultConnection, and EmailSettings__Port maps to EmailSettings:Port.

Environment variables are especially useful in production, containers, and CI/CD pipelines because deployment settings can be changed without modifying application files.

Code Example

// Environment variable:
// EmailSettings__Port=587

int port = builder.Configuration.GetValue<int>("EmailSettings:Port");