In ASP. Three runtime environment variables are provided by default, by looking at the hosting source code we can see, respectively, development, Staging, Production
public static class environmentname{public static readonly string development = "development"; public static readonly String Staging = "Staging"; public static readonly String Production = "Production";}
When you start an ASP. NET core application, you determine which environment should be running. By default, if no environment variable is specified, it is automatically default to production
public class hostingenvironment:ihostingenvironment, extensions.hosting.ihostingenvironment{public string environmentname {get; set;} = Hosting.EnvironmentName.Production; public string ApplicationName {get; set;} public string Webrootpath {get; set;} Public Ifileprovider Webrootfileprovider {get; set;} public string Contentrootpath {get; set;} Public Ifileprovider Contentrootfileprovider {get; set;}}
In the newly created ASP. NET Core Web application, we see two configuration files, namely Appsettings.json and Appsettings.Development.json. In fact, we can add two files, respectively, appsettings. Staging.json and Appsettings.Production.json, respectively, are pre-production and production environments. Depending on the name of the environment variable, the specific configuration file is loaded.
Config. Addjsonfile ("Appsettings.json", Optional:true, Reloadonchange:true) . Addjsonfile ($ "appsettings.{ Env. Environmentname}.json ", Optional:true, Reloadonchange:true);
These files can be understood in the form of a face object, Appsettings.json as the parent class, and several other files as subclasses, when the same node is defined in two configuration files, the configuration of the subclass will prevail, equivalent to Orverwrite. If a node is not found in the corresponding configuration file, it is looked up from the parent class.
If our application is running in a Docker container, Docker also allows the use of env to specify environment variables in Dockerfile
From Microsoft/aspnetcore:2.0.5workdir/appexpose 80COPY. . ENV aspnetcore_environment productionentrypoint ["dotnet", "WebApplication1.dll"]
Another way is to use the-e parameter when you run the container
Docker run-e aspnetcore_environment=development-p 5000:5000 <image>:<tag>
Talking Appsettings.json in ASP.