liveBook · Manning

JSON file provider

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

A JSON provider is a configuration provider that reads application settings from JSON-formatted files.

A JSON file provider loads application settings from JSON-formatted files into ASP.NET Core configuration. WebApplicationBuilder adds appsettings.json and the environment-specific appsettings.ENVIRONMENT.json automatically, so most applications can use these defaults without extra setup. section 10.2.1

You can add a provider explicitly with AddJsonFile(). The optional setting determines whether a missing file is ignored or causes a FileNotFoundException. Providers contribute key-value pairs to IConfiguration, and later providers replace earlier values when keys overlap. section 10.2.1 section 10.2.2

Load and reload appsettings.json

WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Configuration.Sources.Clear(); builder.Configuration.AddJsonFile( "appsettings.json", optional: true, reloadOnChange: true); WebApplication app = builder.Build(); app.MapGet("/", () => app.Configuration.AsEnumerable()); app.Run(); This example starts with no default providers, adds an optional JSON file, and rebuilds IConfiguration when the file changes. The application can then inspect the resulting settings through IConfiguration. section 10.2.1 section 10.2.4

Use JSON providers to:

  • Keep shared settings in one JSON file and application settings in another. section 10.2.2
  • Place environment variables after JSON providers so sensitive or deployment-specific values override file settings. section 10.2.2
  • Change settings such as logging levels while the application is running, when file reloading is enabled. section 10.2.4
Listing 10.3 Loading appsettings.json by clearing the configuration sources
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
 
builder.Configuration.Sources.Clear();                                  #1
builder.Configuration.AddJsonFile("appsettings.json", optional: true);  #2
 
WebApplication app = builder.Build();
 
app.MapGet("/", () => app.Configuration.AsEnumerable());                #3
 
app.Run();
#1 Clears the providers configured by default in WebApplicationBuilder
#2 Adds a JSON configuration provider, providing the name of the configuration file
#3 Returns all the configuration key-value pairs for display purposes

Use the annotated listing to see the complete provider replacement flow: clear the defaults, add appsettings.json, and expose the resulting configuration. section 10.2.1

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

How deep it goes
Explains defaults, optional files, provider ordering, and reload behavior, but defers detailed options binding and secret-management mechanics.
Examples
Uses appsettings.json, sharedSettings.json, environment variables, and a reloadable configuration endpoint.
What it assumes
Assumes familiarity with Program.cs and WebApplicationBuilder while establishing configuration providers and IConfiguration behavior.
What you can do after
Apply it via AddJsonFile to load optional settings, control precedence, and reload configuration when files change.
How it connects
Combines with ConfigurationManager, IConfiguration, environment variables, and strongly typed options.

Also explained in

Compare how 2 books explain JSON file provider →

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.