How to use multiple appsettings.json files in .net core webapi project for different environments
Managing configuration is one of the most important parts of any .NET Core application.
ASP.NET Core provides a powerful configuration system that allows you to load multiple configuration files, override settings per environment, and keep secrets out of source control.Why Use Multiple AppSettings Files?
Advantages
1. Cleaner separation between environments
Each environment has its own config file:
-
dev database
-
test API URLs
-
production connection strings
2. No need to modify code during deployment
Switch environment → settings automatically apply.
3. Safe and secure deployments
Production secrets never appear in development files.
4. Works cleanly with CI/CD & Docker
Different settings per environment, same codebase.
5. Supporting reloadOnChange
You can update JSON settings without restarting the app (in Development).
Disadvantages
1. Requires proper environment variable setup
If ASPNETCORE_ENVIRONMENT is not set correctly, the wrong config loads.
2. More files to maintain
Multiple environments = more configuration files.
3. Sensitive data risk
If production settings accidentally get committed to Git → security issue.
Use Secret Manager or Azure Key Vault for secrets.
4. Reload doesn’t work in Production (IIS) for in-process apps
reloadOnChange is ignored by IIS for in-process hosting.
Step by step example
1. Created .net core webapi project.
2. Added required nuget packages for entity framework core and sql server.
(Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.Tools)
3. Created three database for Development, QA and Live environment in SQL server. Run dbcontext- scaffold command to generate class from database table and context.
or you can use migration to generate database.
4. Add two more appsettings json files in the project root folder. By default when you create project appsetting.json and appsetting.Development.json file are created.
a. appsettings.QA.json
b. appsettings.Live.json
6. Added connection string for each environment in respective appsettings json file.
7. Debugging/Development time set the environment variable in launchsettings.json file.
"ASPNETCORE_ENVIRONMENT": "Development" / "QA" / "Live"
or
you can set ASPNETCORE_ENVIRONMENT variable by right click on solution file and click on properties page. In Property pages click on Debug => General => Debug Launch Profile UI, following popup will open you can set your variable value
8. In Program.cs file, added code to set the connection string.
9. Run the project and verify the connection string is picked from the correct appsettings json file based on the environment variable value set.
10. After deployment to respective environment, set the environment variable in the hosting server environment variable.
or
set environment variable in the iis server for respective application pool.
or
set environment variable in web.config file for respective environment.(recommended)
We will set environment variable in web.config file.
Sample web.config file
11. Run the project and verify the connection string is picked from the correct appsettings json file based on the environment.
For more detail you can check youtube video : Click to watch video
Comments
Post a Comment