You may encounter this situation during development:
I have multiple production environments, such as Huawei cloud server, Alibaba cloud server, Tencent cloud server, etc., or virtual machine 1, virtual machine 2, etc. Each environment corresponds to a different appsettings.json file.
What I need here is to put the release package in a different environment and it will automatically correspond to different appsettings.
How to automatically identify the environment and respond? It's actually very simple, but the tutorials on the Internet are copied and copied, and they are copied, which makes a simple question confusing. The following is the official speech.
1. First set the environment variables of the operating system
I am using Windows display here
Variable names are not case sensitive, but need to be spelled correctly.
The value corresponds to appsettings.xxx.json.
2. Publish to the corresponding environment and then
Add this code to program.cs to test, and start the release package in the corresponding environment.
End summary:
In this way, there is no need to manually replace the configuration file in a production environment, and it can be prepared and organized in advance.
If the environment variable is not configured, .Net will default the environment variable aspnetcore_environment of the current system to Production.