How to set up ASP.NET Core Environment for Deploy/Publish
Setting up different host environments is an important and essential aspect of application development. Today in this article, we will see how to set up ASP.NET Core Environment for Publish.
It’s regularly helpful to set a particular Host Environment for Development purposes or for Testing or Staging or Production purposes.
Today in this article, we shall understand and highlights main aspects of the Environment setup.
Today in this article, we will cover below aspects,
We shall see how to setup environment locally and globally.
Setting up Environment Locally
These local settings are often necessary for debugging purposes or temporary in nature.
Such environment settings can be set using the command line or Visual studio runtime environment or using launch settings.
- Command-line
- Visual studio runtime environment or using
- Launch settings
Command-prompt
set ASPNETCORE_ENVIRONMENT=Staging dotnet run your-application
PowerShell command
$Env:ASPNETCORE_ENVIRONMENT = "Staging" dotnet run your-app
The command prompt or PowerShell commands are effective for the application/processes that are run in the same session of the command prompt.
Any settings once applied can not be changed while the application is running.
It’s always the last HostEnviornment settings the application reads decides the application’s HostEnviornment.
If using Mac OS please use the below commands to set up environment variable using Bash shell,
export ASPNETCORE_ENVIRONMENT=Staging
Set Environment for Local Debugging purpose
This can be done using Visual Studio or VScode editor easily,
- In VSCode Use .vscode/launch.json for setting the environment for debugging purposes.
- In Visual Studio use launchSettings.json or use Project->Properties->Debug->Enviornment Variable to set the environment for debugging purposes.
Dynamically Setting environment
You can easily set environment-specific configuration easily using
Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "TEST");
Above settings will be limited for temporarily for executing the current process.
Setting up Environment Globally
The above-discussed techniques are used for setting the environment temporarily for executing the current process with the given environment configuration. These settings will not remain effective when the new session of the command prompt is used.
In such cases to set the value globally use either of the following approaches if using Windows,
Using Control Panel
Open the Control Panel > System > Advanced system settings
Using Command Line
Command prompt
Open a command prompt or PowerShell command prompt in administrative mode.
Use the below command,
setx ASPNETCORE_ENVIRONMENT Production /M
The /M
switch lets you set the environment variable at the system level.
PowerShell command
[Environment]::SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Staging", "Machine")
In the above option “Machine
” set the environment variable at the system level. If used “User
” option, the environment variable will be set for the user account only.
Setting up environment variable in CLOUD
For a Cloud-hosted environment, please set up the environment variable using their specifc way of managing the custom environment variable or user-defined variable, or system variables.
Kindly refer below article for more details,
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.