'How to generate the appsettings.<EnvironmentName>.json file?

I have an ASP.NET Core 2 WebAPI which will be deployed across the following environments:

INT, QA, STAGE, PRODUCTION environments.

Based on the above, I need to have appsettings.<EnvironmentName>.json file for each environment. From the link : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-2.1, I see that

In case of local development environment, the Environment Variable called ASPNETCORE_ENVIRONMENT is set to Development. In case of the deployment where ASPNETCORE_ENVIRONMENT is not set, the default is Production

I would like to know what are the steps required to take care while preparing the appsettings.<EnvironmentName>.json file for INT, QA and STAGE environment. Do I need to set the environment explicitly for each environment web server:

set ASPNETCORE_ENVIRONMENT=Development.

Can anyone help me to by providing their guidance?



Solution 1:[1]

I might be missing something, but this seems pretty straight-forward. To create the environment-specific setting files, you literally just create them in your project, i.e. create a new file called appsettings.Foo.json in the root of your project. Visual Studio will automatically put it under appsettings.json as a parent in the Solution Explorer.

On the server, yes, you'd need to set the environment you want to run under to ASPNETCORE_ENVIRONMENT. If it's supposed to use appsettings.QA.json, then set it to QA. That's all there is to it.

Solution 2:[2]

You will have to set the ASPNETCORE_ENVIRONMENT on the server with the configuration you wish to host the app.

Take a look at the sample project provided by Microsoft on Github

Sample Environment project

Solution 3:[3]

First, in Visual Studio you will have to right click your project name and add a new item.

Inside 'Add new Item' window chose 'Scripts' on the left column, and look for JavaScript JSON Configuration File template.

Name it as appsettings.Production.json, for example, and click 'Add'. It's going to connect automatically to appsettings.json.

To set which environment your application runs on, just set up the ASPNETCORE_ENVIRONMENT environment variable.

Set the variable through the command prompt by typing set ASPNETCORE_ENVIRONMENT=Production in Windows or export ASPNET_CORE_ENVIRONMENT=Production in Linux.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chris Pratt
Solution 2 codingpirate
Solution 3 marc_s