appsettings.json in ASP.NET Core: Layering, Options and Secrets

6 min·Nov 21, 2026
AI Summary

appsettings.json is the base layer of ASP.NET Core configuration. Later sources override earlier ones: appsettings.json, then appsettings.{Environment}.json, then user secrets (Development only), then environment variables, then command-line args. Nested keys map to environment variables with a double underscore (Smtp__Port). Bind sections to typed options with AddOptions<T>().Bind(...).ValidateOnStart(). Keep secrets out of the file: use user-secrets locally and Key Vault or environment variables in production.

.NET appsettings.json
An ASP.NET Core appsettings.json with logging levels, connection strings, AllowedHosts, Kestrel endpoints and a strongly typed options section. It is the base layer of .NET configuration.
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "example.com;*.example.com",
  "ConnectionStrings": {
    "Default": "Host=localhost;Port=5432;Database=app;Username=app"
  },
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://0.0.0.0:8080"
      }
    }
  },
  "Smtp": {
    "Host": "smtp.example.com",
    "Port": 587,
    "UseStartTls": true,
    "From": "[email protected]"
  },
  "FeatureFlags": {
    "NewCheckout": false
  }
}

appsettings.json is where an ASP.NET Core app keeps its settings. It is only the first layer, though. Understanding the layering is most of the job.

The layering order

Each source overrides the ones before it:

  1. appsettings.json
  2. appsettings.{Environment}.json, such as appsettings.Development.json
  3. User secrets (Development environment only)
  4. Environment variables
  5. Command-line arguments

So defaults go in appsettings.json, per-environment tweaks go in the environment file, and secrets come from 3–5 or a vault. Secrets never go in the committed file.

The usual sections

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "example.com;*.example.com", "ConnectionStrings": { "Default": "Host=localhost;Database=app;Username=app" } }
  • Logging.LogLevel: minimum level per category. Use the most specific category name you can.
  • AllowedHosts: host-header filtering. "*" allows any host.
  • ConnectionStrings: read with builder.Configuration.GetConnectionString("Default").

Environment variables for nested keys

The : path separator becomes __:

export ConnectionStrings__Default="Host=db;Database=app;Username=app;Password=..." export Smtp__Port=2525

This works the same way in Docker, Kubernetes and Azure App Service settings.

Typed options instead of string lookups

"Smtp": { "Host": "smtp.example.com", "Port": 587, "UseStartTls": true } builder.Services.AddOptions<SmtpOptions>() .Bind(builder.Configuration.GetSection("Smtp")) .ValidateDataAnnotations() .ValidateOnStart();

ValidateOnStart() makes a missing or invalid value fail at boot, not on the first email.

Kestrel endpoints

"Kestrel": { "Endpoints": { "Http": { "Url": "http://0.0.0.0:8080" } } }

Containers typically listen on 8080 over plain HTTP and let the ingress terminate TLS.

Security notes

  • Locally, run dotnet user-secrets set "ConnectionStrings:Default" "...". It stores values outside the repo.
  • In production, use environment variables, Key Vault references or a managed identity, never a committed password.
  • Avoid Debug log levels in production. They can log request data.

Open the appsettings template and ask the workspace to "add a Redis connection string" or "lower Microsoft.AspNetCore logging to Warning", then review the diff.

FAQ

How do I override a nested appsettings value with an environment variable?

Join the path with a double underscore: Smtp:Port becomes Smtp__Port, and ConnectionStrings:Default becomes ConnectionStrings__Default. On Azure App Service, app settings use the same naming.

Which appsettings file is used in production?

appsettings.json is always loaded, then appsettings.{Environment}.json for the current ASPNETCORE_ENVIRONMENT (or DOTNET_ENVIRONMENT). It defaults to Production when unset, so appsettings.Production.json applies.

Can appsettings.json have comments?

Yes. The .NET JSON configuration provider accepts comments and trailing commas, but keep the file valid JSON if other tools read it too.