ASP.NET Core has built-in support for multiple environments. This makes it easy to load different configuration and apply different middleware depending on the environment.
The typical to control the environment we want to use is through the ASPNETCORE_ENVIRONMENT
environment variable.
It is also possible to set the environment variable by passing it to the dotnet run
command as an argument.
To set this up, we have to modify the Program.cs
:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IHostBuilder CreateHostBuilder(string[] args) => | |
Host.CreateDefaultBuilder(args) | |
.ConfigureHostConfiguration(configHost => | |
{ | |
configHost.AddCommandLine(args); | |
}) | |
.ConfigureWebHostDefaults(webBuilder => | |
{ | |
webBuilder.UseStartup<Startup>(); | |
}); |
The AddCommandLine method allows us to read configuration values from the command line.
Now we can start the app with dotnet run --environment Development.