liveBook · Manning

Appsettings.json

As explained in Building Web APIs with ASP.NET Core

appsettings.json is a .NET application configuration file that stores settings as JSON key-value pairs shared across runtime environments unless overridden.

appsettings.json stores an ASP.NET Core app’s shared configuration as JSON key-value pairs. In a web API project, it provides defaults for every runtime environment. A file such as appsettings.Development.json can add settings or override matching keys when the app runs in Development. section 2.4.2

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
"UseDeveloperExceptionPage": false
}

The JSON listing shows grouped logging settings alongside a boolean UseDeveloperExceptionPage setting set to false. section 2.4.2

To configure behavior by environment:

  • Set shared defaults in appsettings.json, then put Development-only overrides in appsettings.Development.json. The environment-specific file is read afterward, so its matching values take precedence. section 2.4.2
  • Add a staging configuration file when Staging needs different values. For example, keep UseDeveloperExceptionPage false while setting UseSwagger true; these settings do not change behavior until the app uses them. section 2.5.2

Switch the error response

Program.cs can read app.Configuration.GetValue<bool>("UseDeveloperExceptionPage") to choose between DeveloperExceptionPageMiddleware and an exception handler. With the shared value set to false, start the app and visit /error/test: the thrown test exception produces a ProblemDetail JSON response. Put true in the Development-specific file when developers need the detailed exception page, rather than enabling it for every environment. section 2.4.3 section 2.5.2

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains JSON settings, override order, and a Program.cs boolean read; defers production-server environment setup.
Examples
Uses a developer exception page toggle, a thrown-error test endpoint, and staging and development Swagger settings.
What it assumes
Introduces runtime environments before applying JSON settings and starts from an autogenerated ASP.NET Core web API project.
What you can do after
Operate/configure it to set shared defaults and environment overrides, then verify an exception response through a test endpoint.
How it connects
Links runtime environments, environment variables, launchSettings.json, Program.cs, and exception-handling middleware.

Also explained in

Compare how 4 books explain appsettings.json →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.