Menu Close

ASP.NET Core environment variables

In this article we are going to discuss about ASP.Net Core Environment Variables. Please go through our previous article about Static Files 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;

Environment Variables

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

Static Files launch settings

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.

You can go through below video to see more deatils.

Conclusion

Leave behind your valuable queries and suggestions in the comment section below. Also, if you think this article helps you, do not forget to share this with your developer community. Happy Coding 🙂

Jayant Tripathy
Coder, Blogger, YouTuber

A passionate developer keep focus on learning and working on new technology.

Leave a Reply

Your email address will not be published. Required fields are marked *