In this article we are going to discuss about ASP .NET Core Environment Variables. Please go through our previous article about How to Create Custom Middleware in ASP.NET Core.

In professional application development, there are multiple phases where an application is tested before publishing it to the real users. Typically have follow the below environments levels.

  • Development
  • Staging
  • Production

We as a 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.

ASP.NET Core uses an environment variable called ASPNETCORE_ENVIRONMENT to indicate the runtime environment.

The value is case insensitive in Windows and Mac OS but it is case sensitive on Linux

Let’s discuss more about the different type of environments variables.

Development Environment Variables

This is the most crucial environment of a software application development, where we use developer exception page if there is an unhandled exception so we can understand the root cause of the exception and fix it if required. So before going the application to other environment it should be error free.

Staging Environment Variables

Many organisations, usually setup their staging environment to interface with the service providers as well, for complete end to end testing. If there is an unhandled exception, display user friendly error page instead of the developer exception page. The generic message look like below

“Something went wrong, Please contact the Service administrator”

Production Environment Variables

This is the actual live environment. Production environment should be configured for maximum security and performance.

How to Set these Environment Variables in Visual Studio

Well, in Visual Studio we can set the ASPNETCORE_ENVIRONMENT in the debug tab of project properties like below;

CoreSpider - ASPNETCORE_ENVIRONMENT GUI

Also You may change the value as per your need. This value will be saved in the launchSettings.json file as shown below.

CoreSpider-launchSettings.json
Access Environment Variable at Runtime

The IHostingEnvironment 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().

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            // If the environment is Development serve Developer Exception Page
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            // else serve User Friendly Error Page with Application Support Contact Info
            else if (env.IsStaging() || env.IsProduction())
            {
                app.UseExceptionHandler("/Error");
            }
            if (env.IsProduction())
            {
                // code to be executed in production environment 
            }

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

Video Tutorial

Conclusion

This article explain about Environment Variables in ASP .NET Core. If have any doubt or concern about this article then please give your feedback.

Pin It on Pinterest