ASP.NET Core Environment Variables: A Comprehensive Guide

ASP.NET Core Environment Variables: A Comprehensive Guide

In this article, we will delve into the world of ASP.NET Core environment variables, exploring their configuration, access, and usage in various development environments.

Why Do We Need Different Development Environments?

In most software development organizations, we have multiple development environments, each serving a specific purpose:

  • Development Environment: This is where our software developers perform their daily development work. We want to load non-shrink JavaScript and CSS files for debugging purposes, and if an unhandled exception occurs, we need a developer exception page to identify the root cause of the anomaly.
  • Demo Environment: Many organizations aim to create a presentation environment that is as consistent as possible with the actual production environment. The main reason for this environment is to identify any problems associated with deployment. In addition, if you are developing a B2B application, you might be connecting with other service provider systems. We usually set up a temporary environment to interact with the service provider for end-to-end testing. We do not normally troubleshoot and debug in a demo environment, and to obtain better performance, we need to reduce the load of JavaScript and CSS files. If an unhandled exception exists, a user-friendly error page is displayed instead of the developer exception page.
  • Production Environment: This is our actual environment for daily operations. The production environment should be configured for maximum security and performance. Therefore, the reduced load JavaScript and CSS files improve performance, and a user-friendly error page is displayed rather than the developer exception page. Technical details on the Developer Exception page do not make sense for the end user, malicious users can use their access to your application.

Configuration Variables ASPNETCORE_ENVIRONMENT

We use this variable to set the environment for the application. In our local development machine, we usually set this environment variable in the launchsettings.json file. If required, we can also set it in the operating system.

Setting on the Windows Operating System

  1. Open the Windows Control Panel.
  2. In the “Control Panel” window, type in the upper right corner of the “Search Control Panel” text box “environment.”
  3. Click “Edit System environment variables” link.
  4. In the pop-up “System Properties” window, click the “Environment Variables” button.
  5. In the pop-up “Environment Variables” window, under the section “System Variables,” click “New” button.
  6. In the pop-up “New System Variable” window, enter a value in the “Tag Name” text box ASPNETCORE_ENVIRONMENT, and in the “Variable Value” text box, enter Development.
  7. Click “OK” to close all the pop-up windows.

Accessing ASPNETCORE_ENVIRONMENT Variable Values

Out of the box, ASP.NET Core provides IHostingEnvironment services, which we can use to access ASPNETCORE_ENVIRONMENT variable values. Looking at the example we have been using an application, pay attention to the Configure() method in the Startup.cs file. The IHostingEnvironment service has been injected into this method.

Commonly Used Method IHostingEnvironment Service Description

We can use the following method to identify environmental services IHostingEnvironment:

  • IsDevelopment()
  • IsStaging()
  • IsProduction()

If you have a UAT (user acceptance testing) or QA (quality assurance) environment, you can customize the environment by using the IsEnvironment() method.

Use the Following Method to Identify Environmental Services IHostingEnvironment Run the Application

  • IsDevelopment()
  • IsStaging()
  • IsProduction()
  • IsEnvironment()

For example, to check whether the UAT environment, use the IsEnvironment() method as follows:

if (env.IsEnvironment("UAT"))
{
    // If the environment is Development, serve Developer Exception Page
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    // else has to provide application support contact information
    else if (env.IsStaging() || env.IsProduction() || env.IsEnvironment("UAT"))
    {
        app.UseExceptionHandler("/Error");
    }
}

Tag Helpers is a New Feature in ASP.NET Core

In a Razor view inside, it can also be used in the .cshtml page, known as environmental labeling assistant. This environmental labeling program helps support according to ASPNETCORE_ENVIRONMENT present different content value of the variable. When we learn this lesson and create the model, view, and controller for our application, we will discuss in detail Tag Helpers, including environmental labeling Assistant (Environment Tag Helper).

Conclusion

In this article, we have explored the concept of ASP.NET Core environment variables, their configuration, access, and usage in various development environments. We have discussed the importance of different development environments, such as development, demo, and production environments, and how to set up the ASPNETCORE_ENVIRONMENT variable in the operating system. We have also explored the commonly used method IHostingEnvironment service description and how to use it to identify environmental services. Finally, we have introduced Tag Helpers, a new feature in ASP.NET Core, which can be used in Razor views to display different content values based on the ASPNETCORE_ENVIRONMENT variable.