When .Net 6 is published to multiple different environments, configure appsettings.json and environment variable tutorials

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. insert image description here
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

step 1

insert image description here
insert image description 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.
insert image description here
insert image description here

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.

Guess you like

Origin blog.csdn.net/sinat_41844660/article/details/127360940