Multiple appsettings.json in .net core without using an environment variable

Well, the internet is filled with a lot of ideas of having multiple appsettings.json files based on the environment but all of them rely on having an environment variable in the SERVER.

This idea is great if you have one server dedicated to one .net core application, which is usually not the case.

Now imaging how do you achieve this in case of a single server that hosts multiple .net core application with multiple environments.

Let’s jump to the solution.

The detailed video explaining the problem statement and the solution

The trick is to make a small tweak in the Program.cs file of your .net core application so that it accepts multiple appsettings.json files.

Now our .net core application is ready to accept multiple appsettings.json files. I now have 3 configuration files in my app.

Now the only part left is to configure our environments dynamically. This is how the dev & QA appsettings will look like

Below is the configuration file which decides which app settings to pickup.

What exactly happened: Your Program.cs file asked the appsettings.json what is the active environment? Based on the appsettings.json -> Environment configuration the Program.cs loaded appsettings.{_env}.json ie appsettings.dev.json

Now the application is loaded with all the configurations from appsettings.dev.json

This is how I managed to handle multiple app settings in .net core

I’m a c# & Angular developer with a passion and love for all the programming languages. Youtube — http://youtube.com/c/technosaviour