ASP.NET Core uses the ASPNETCORE_ENVIRONMENT
environment variable to determine the current environment. By default, if you run your application without setting this value, it will automatically default to the Production
environment.
> dotnet run
Project TestApp (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
Hosting environment: Production
Content root path: C:\Projects\TestApp
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Setting the environment variable in Windows
At the command line
You can easily set an environment variable from a command prompt using the setx.exe command included in Windows. You can use it to easily set a user variable:
>setx ASPNETCORE_ENVIRONMENT "Development"
SUCCESS: Specified value was saved.
Note that the environment variable is not set in the current open window. You will need to open a new command prompt to see the updated environment. It is also possible to set system variables (rather than just user variables) if you open an administrative command prompt and add the /M switch:
>setx ASPNETCORE_ENVIRONMENT "Development" /M
SUCCESS: Specified value was saved.
Using PowerShell
Alternatively, you can use PowerShell to set the variable. In PowerShell, as well as the normal user and system variables, you can also create a temporary variable using the $Env:
command:
$Env:ASPNETCORE_ENVIRONMENT = "Development"
The variable created lasts just for the duration of your PowerShell session - once you close the window the environment reverts back to its default value.
Alternatively, you could set the user or system environment variables directly. This method does not change the environment variables in the current session, so you will need to open a new PowerShell window to see your changes. As before, changing the system (Machine) variables will require administrative access
[Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development", "User")
[Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Development", "Machine")
Using the windows control panel
If you're not a fan of the command prompt, you can easily update your variables using your mouse!Click the windows start menu button (or press the Windows key), search for environment variables
, and choose Edit environment variables for your account:
Selecting this option will open the System Properties dialog
Click Environment Variables to view the list of current environment variables on your system.
Assuming you do not already have a variable called ASPNETCORE_ENVIRONMENT
, click the New... button and add a new account environment variable:
Click OK to save all your changes. You will need to re-open any command windows to ensure the new environment variables are loaded.