ASP.NET CORE Configuration Environment Variables

In this tutorial will discuss how configure environment variables and use of the environment variables.

Typically, in professional application development, there are multiple phases where an application is tested before publishing it to the real users. These phases by convention are development, staging, and production. We as developers might like to control the behavior of an application based on the phases the application is in. Environment variable indicates the runtime environment in which an application is currently running.

Why do we need different Environments like Development, Staging, Production etc.

Development Environment : We software developers typically use this environment for our day to day development work. We want non-minified JavaScript and CSS files to be loaded on a development environment to make debugging easy.

Staging Environment : Most of the organizations, try to keep their staging environment as identical as possible to the actual production environment. The primary reason for this environment is to identify any deployment related issues and complete end to end testing. We usually do not troubleshoot or debug on a staging environment, so for better performance we want minified JavaScript and CSS files to be loaded.

Production Environment : The actual live environment and that should be configured for maximum security and performance. So load minified JavaScript and CSS files to improve the performance. For better security, display a User Friendly Error Page instead of the Developer Exception Page. The technical details on the Developer Exception Page does not make sense to the end user and they can be used by malicious user to break into your application.

ASP.NET Core uses an environment variable called ASPNETCORE_ENVIRONMENT to identify the runtime environment. The value of this variable can be anything as per your need but typically it can be Development, Staging, or Production. The value is case insensitive in Windows and Mac OS but it is case sensitive on Linux.

In Visual Studio, we can set ASPNETCORE_ENVIRONMENT in the debug tab of project properties. Open project properties by right clicking on the project in the solution explorer and select Properties.

We can change the value as per your need. This value will be saved in the launchSettings.json file as shown below.

How to set environment variable in staging and production

In a staging or production environment we typically set this environment variable in the operating system.

We usually set this variable to one of the following values depending on the environment in which our application is hosted and running

      1. Open Windows Control Panel

      2. In the Control Panel window, type “environment” in the “Search Control Panel” text box on the top right hand corner

      3. Click on “Edit the system environment variables” link
        set environment variable on windows

      4. In the “System Properties” window that pops up, click on “Environment Variables” button
        set asp.net core environment variables on windows

      5. On the “Environment Variables” window that pops up, click on “New” button under “System variables” section

      6. In the “New System Variable” window that pops up, type

        • ASPNETCORE_ENVIRONMENT in “Variable name” text box

        • Development in “Variable value” text box
          set aspnetcore_environment windows

      7. Click “OK” to close all the pop up windows

 NoteIf we have not explicitly set ASPNETCORE_ENVIRONMENT variable, it defaults to Production. This is done on purpose for better security and performance. 

How to access variables.

The IWebHostingEnvironment service is provided by ASP.NET hosting layer and can be used anywhere in your application via Dependency Injection. This example shows how we can check the environment variable is development in the Configure method of Startup class.

We can get the value of an environment variable in our code to execute some additional code based on its value. The IWebHostingEnvironment service includes EnvironmentName property which contains the value of ASPNETCORE_ENVIRONMENT variable. ASP.NET Core also includes extension methods to check the environment such as IsDevelopment(), IsStating(), IsEnvironment() and IsProduction().

 
 

You can watch our video version of this tutorial with step by step explanation.