Introduction to asp.net core environment variables
ASP.NET Core Environment Variables uses the variable called ASPNETCORE_ENVIRONMENT, which points towards the runtime environment. The value of this variable may be something as per the requirement but usually be a Production, Staging, and Development. The Environment variable defines the runtime environment for which the application is currently running. The value is not case-sensitive for Windows and Mac OS, but for Linux, it is case-sensitive.
ASP.NET Core Environment Variable Overviews
The ASP.NET Core supplies with a selection to manage various environments for an Application. The Enterprise or Professional Application has several environments or phases (stages) before deploying the Application to the production or to the real users. Various Environments like Staging, Production and Development Environment. The Environment variable defines the runtime environment for which the application is currently running.
The ASP.NET Core Application makes use of an Environment Variable called ASPNETCORE_ENVIRONMENT, which indicates the application is presently running in. You can afford any of the values to the variable as per the need, but in general, the environment values of Development, Production, or Staging can be used. The environment name is not case sensitive; lets us consider one example if we set the variable to Development or DEVELOPMENT or development, the concluding result will be the same.
In ASP.NET Core, there will be enhanced support for managing applications around several environments like production, staging, and development. Mainly, the Environment Variables are used to specify the Environment application is executing, enabling the application to configure it.
For the convention purpose, there will be used by three values; those variables can be set to any of the values you prefer they are Development, Production, Staging.
This process will be used during the development of the application; while using Visual Studio, the setting will be denoted in the project’s debug like IIS Express and so on.
The Staging Environment is a Pre-Production Environment used for testing at the completion of the application before the deployment.
The Production Environment is used while the application executes during the live process where the end-user is working on it. This process is configured to application Robustness, based on performance and with safe and secure. Some settings of production will be varied from development they are,
- Allow the Production Logging and Controlling
- Caching- turn on
- Diagnostic Error Pages –turn off
- Make sure of entire client-side resources which are safely served from CDN.
Lets the following code:
- It calls to UserDeveloperExceptionPage when ASPNETCORE_ENVIRONMENT is set to Development.
- It calls to UserExceptionHandler when the value of ASPNETCORE_ENVIRONMENT sets to Production, Staging, or Staging_2.
- It injects to IWebHostEnvironment into the Startup.Configure. This method is functional when the app needs altering Startup.Configure for a few environments with the least code differences as per the environment.
ASP.NET Core Environment Variable Command
To set the hosting environment by using the commands, depending on configuring the WebHostBuilder, we can specify the environment by offering the command line argument. To make use of ConfigurationBuilder, it makes use of the AddCommandLine() extension method from Microsoft.Extensions.Configuration.CommandLine package. Now then pass the configuration to WebHostBuilder by using the UseConfiguration(config):
Var config= new ConfigurationBuilder()
Var host=new WebHostBuilder().UserConfiguration (config)
. UseContentRoot (Directory.GetCurrentDirectory ())
. Build();It enables to specify the environment hosting during runtime by using the environment argument, àdotnet run – environment "Staging"
Project in the solution explorer and select Properties
ASP.NET Core uses the Environment Variable called ASPNETCORE_ENVIRONMENT, which indicates the runtime environment. The value of this variable may be something as per the requirement but usually be a Production, Staging, and Development. The Environment variable defines the runtime environment for which the application is running currently. The value is not case-sensitive for Windows and Mac OS, but for Linux, it is case-sensitive.
Look at the following screens, Open Visual Studio, and set ASPNETCORE_ENVIRONMENT in debug tab of Project Properties. Just open the Project Properties just by right-clicking the project in the Solution Explorer and choosing the Properties,
Once opening the Project Properties, it will open the Properties Page, just click on the Debug tab and look at the Environment Variables as shown below,
- It contains the Profile Settings
- It is used only on the Local Development Machine
- It is not deployed
Look at the following JSON, which depicts the launchSetting.json file for ASP.NET Core Web Project, which is named as EnvironmentSample created with Visual Studio or dotnet new,
JSON Code – launchSetting.json
We can also make a change directly in the Environment Variable in launchSetting.json.
How asp.NET Core Support Environment Configurations
The Environment variable defines the runtime environment for which the application is running currently. It makes available with the Static Class to access the Environment Variables Environment in System NameSpace. Let’s see the following variable as given below,
static void Main(string args)
var environment = Environment.GetEnvironmentVariable("windir");
Console.WriteLine("windir path: " + environment);
You can able to see the value of the “windir” Environment Variable through the “C:\WINDOWS.”
While using the VISUAL STUDIO, the Environment Variable can be denoted during the Development Process in the Projects’ Debug File; let’s see the following example as shown.
ASP.NET Core Environment Variable Examples
The ASP.NET Core supplies with a selection to manage various environments for an Application. To get the value of Environment Variable in the code to execute with the extra code based on some value. IHostingEnvironment Service, which includes the EnvironmentName Property, which contains the value of ASPNETCORE_ENVIRONMENT variable, it also consists of the extension methods to verify the environments like IsDevelopment(), IsEnvironm`ent(), IsStaging () and ISProduction(). Let’s look at the sample code below as shown,
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
// Development Environment - Code Execution
// Development Environment - Code Execution
// Staging Environment - Code Execution
// Production Environment - Code Execution
This article shows the asp.net core environment variables, which indicates during the runtime environment. The environment variable defines the runtime environment for which the application is running currently. I hope the article helps you to understand the concept.
This is a guide to asp.net core environment variables. Here we discuss the ASP.NET Core Environment Variable, which indicates during the runtime environment. You may also have a look at the following articles to learn more –