ConfigurationManager builds and exposes an ASP.NET Core app’s settings. It implements IConfigurationBuilder for adding configuration providers and IConfigurationRoot for holding their values. Application code generally reads those values through IConfiguration. section 10.2
Figure 10.2 Using ConfigurationManager to populate IConfiguration. Configuration providers are added to the ConfigurationManager with extension methods. The manager queries the provider and adds all the returned values to the IConfigurationRoot, which implements IConfiguration.
Follow how providers supply key-value pairs to ConfigurationManager and how the resulting values become available through IConfiguration. section 10.2
From .NET 6 onward, WebApplication.CreateBuilder(args) creates a ConfigurationManager available as builder.Configuration. It already includes providers for appsettings.json, an environment-specific JSON file, User Secrets, environment variables, and command-line arguments. You can add providers or replace the defaults. section 10.2.1
Read a setting in Program.cs
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); var zoomLevel = builder.Configuration["MapSettings:DefaultZoomLevel"]; The colon separates levels of a nested configuration key. To replace the default sources, call builder.Configuration.Sources.Clear(), then add the sources you want—for example, AddJsonFile("appsettings.json", optional: true). Clearing sources removes the defaults, so keep them unless you mean to replace them. section 10.2.1
Provider order matters: a later provider replaces an earlier value with the same key while leaving other values intact. An optional environment-specific JSON file can override appsettings.json; environment variables can override file defaults at runtime. Use User Secrets for local development and environment variables for production secrets rather than committing secrets to source control. In environment-variable names, use a double underscore for a nested key where a colon is not allowed; read the resulting key with a colon through IConfiguration. section 10.4.2 section 10.2.3
