There are two sets of configuration for an ASP.NET Core application:
- The configuration for the WebHost.
- The configuration for the application itself.
First the WebHost configuration is built (source):
_config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
As the source code shows, the configuration for the WebHost uses only environment variables and only those that are prefixed with ASPNETCORE_
. One of those environment variables is, unsurprisingly, ASPNETCORE_ENVIRONMENT
.
A little later down the line, an implementation of IHostingEnvironment
is instantiated. This ends up using _config
to retrieve a configuration setting named environment
, which comes from the ASPNETCORE_ENVIRONMENT
variable. If there is no such value, it defaults to Production
.
Next up, the configuration for the application itself gets built. At step 2 in your question, the value of env.EnvironmentName
is from the IHostingEnvironment
I've already mentioned. If you were to set an environment value of e.g. Staging
as a command-line argument, it would not change the value used in the WebHost configuration, as this applies only to the application configuration.
Andew Lock goes into more detail about how this all works and also demonstrates how to configure the WebHost to use additional configuration sources.