Automatically set appsettings.json for dev and release environments in asp.net core?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
ASP.NET Core uses environment-specific configuration files (appsettings.Development.json, appsettings.Production.json) that automatically override values in the base appsettings.json. The framework determines which file to load based on the ASPNETCORE_ENVIRONMENT environment variable. Settings in the environment-specific file merge with the base file, so you only need to specify values that differ per environment. This system eliminates the need for build-time config transforms used in older ASP.NET.
How Configuration Loading Works
The configuration system is additive. appsettings.Production.json does not replace appsettings.json — it merges on top of it. Only keys present in the environment file are overridden.
File Structure
In Development, EnableSwagger is true (overridden) and MaxPageSize is 100 (inherited from base). In Production, EnableSwagger is false (base value) since appsettings.Production.json does not override it.
Setting the Environment
launchSettings.json is only used during local development. In production, set the environment variable on the server, in the Docker container, or in the hosting platform.
Checking the Environment in Code
Strongly Typed Configuration
Bind configuration sections to C# classes with IOptions<T>. The values automatically reflect the merged configuration for the current environment.
Docker and CI/CD
In Docker, use environment variables for secrets instead of appsettings.Production.json. The __ (double underscore) separator maps to the : hierarchy in JSON configuration.
User Secrets for Development
User secrets store sensitive values outside the project directory (in ~/.microsoft/usersecrets/), keeping them out of source control. They are loaded automatically in the Development environment.
Common Pitfalls
- Not setting ASPNETCORE_ENVIRONMENT in production: The default environment is
Production, but many hosting platforms do not set the variable. If middleware behaves unexpectedly, verify the variable is set withapp.Environment.EnvironmentName. - Committing secrets in appsettings.Production.json: Environment-specific files are tracked by git. Use environment variables or a secrets manager (Azure Key Vault, AWS Secrets Manager) for passwords and API keys in production.
- Expecting full replacement instead of merge:
appsettings.Development.jsonmerges withappsettings.json. If the base file has a key, it carries into all environments unless explicitly overridden. To "remove" a key, set it to null or an empty value. - launchSettings.json in production:
launchSettings.jsonis ignored outsidedotnet runand Visual Studio. SettingASPNETCORE_ENVIRONMENTthere has no effect in Docker, IIS, or Kestrel started directly. - Case sensitivity on Linux: File names are case-sensitive on Linux.
appsettings.development.jsondoes not matchDevelopment. Use exact casing:appsettings.Development.json.
Summary
- ASP.NET Core loads
appsettings.jsonfirst, thenappsettings.{Environment}.jsonas an override - Set
ASPNETCORE_ENVIRONMENTtoDevelopment,Staging, orProductionto select the file - Environment-specific files merge with the base file — only overridden keys change
- Use
IOptions<T>for strongly typed configuration that reflects the current environment - Use environment variables or user secrets for sensitive values — never commit passwords to config files
- In Docker, use
ENVordocker-composeenvironment variables with__as the hierarchy separator
Related reading
- Automating deployments of large distributed client server application part of CI / CD
- avoid rebuilding node_modules in elastic beanstalk
- Avoiding INSUFFICIENT DATA in Cloudwatch?
- await population of a stream
- Automating the InvokeRequired code pattern
- Avoiding first chance exception messages when the exception is safely handled
- AWS-ECS vs Kubernetes
- AWS API Gateway VPC Link Pricing

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.