liveBook · Manning

ConfigurationManager

As explained in ASP.NET Core in Action, 3e

ConfigurationManager is the .NET configuration type that builds and exposes application configuration while implementing IConfigurationBuilder and IConfigurationRoot.

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.
CH10_F02_Lock3

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains provider loading, overrides, direct lookups, and environment-specific setup without implementing a custom provider.
Examples
Shows clearing default sources, adding appsettings.json, reading a nested key, and loading an environment-specific JSON file.
What it assumes
Starts from a WebApplicationBuilder setup and defines the configuration interfaces and providers before showing customization.
What you can do after
Operate/configure it by retaining or replacing default providers and reading layered settings through IConfiguration.
How it connects
Connects IConfigurationBuilder and IConfigurationRoot to configuration providers, IConfiguration, WebApplicationBuilder, and hosting environments.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.