Configuration is the set of external settings and secrets that controls an application’s behavior at runtime. A setting might choose a default map location; a secret might be an API key or database connection string. Keeping these values out of application code lets you change them at deployment without recompiling. It also reduces the risk of committing secrets to source control. section 10.1
Settings for a store-location map
A store-location application can configure its default map location and zoom level alongside the connection string for its store database and a mapping API key. The location and zoom level are ordinary settings; the connection string and API key are secrets that should not be hardcoded. section 10.1
ASP.NET Core loads key–value pairs through configuration providers. A ConfigurationManager combines their values into an IConfiguration object that application code can read. Providers can load JSON, XML, INI files, environment variables, or command-line arguments. When providers define the same key, a later provider replaces the earlier value; keys found only in earlier providers remain available. WebApplicationBuilder sets up common providers automatically, so many applications can start with the defaults. section 10.2 section 10.5
Listing 10.2 Adding configuration values to an appsettings.json file
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"MapSettings": { #1
"DefaultZoomLevel": 9, #2
"DefaultLocation": { #3
"latitude": 50.500, #3
"longitude": -4.000 #3
}
}
}
#1 Nest all the configuration under the MapSettings key.
#2 Values can be numbers in the JSON file, but they’ll be converted to strings when they’re read.
#3 You can create deeply nested structures to organize your configuration values better.
The appsettings.json example groups map values under MapSettings. JSON nesting keeps related settings together; its numeric zoom value is converted to a string when read as configuration. section 10.2
Choose how each value should be stored and used:
- For a direct lookup, use IConfiguration with a colon-separated key such as Configuration["MapSettings:DefaultZoomLevel"]. This is useful in Program.cs. section 10.5
- For a feature with several related settings, bind configuration to a strongly typed options object and inject it into services. section 10.0
- Keep production secrets in environment variables rather than a repository file. For development, User Secrets Manager stores them outside the project folder. section 10.5
Hosting environments let the same application select different values for development and production—for example, different database connections and logging levels. File-based providers can also reload configuration when a file changes: setting reloadOnChange to true for AddJsonFile monitors that file and rebuilds IConfiguration as needed. This makes it possible to raise a production logging level while investigating a problem, then lower it again without recompiling or restarting. Reloading is generally not available for environment-variable providers. section 10.4 section 10.2.4






















