Monday, June 1, 2020

ASP.NET Core–Set environment through the commandline

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:

The AddCommandLine method allows us to read configuration values from the command line.

Now we can start the app with dotnet run --environment Development.