Pro Code Guide

Developer’s Guide To Programming


How to configure hosting environment in ASP.NET Core

Updated Feb 3, 2021 | 0 comments

This article will cover the ways to configure hosting environment in ASP.NET Core application based on the environment on which it is deployed as application behavior can be controlled based on the environment.

The hosting environment in ASP.NET Core is used to indicate at runtime on which environment (Development, Staging, or Production) an ASP.NET Core application is running. There can be different values for Hosting Environment in ASP.NET Core for the same application on different machines/servers and it can be set any single value. Framework provided environments are Development, Staging & Production but you can specify your own values as well i.e. Testing, QA, PrePROD, etc.

Environment variables allow the application to be configured as per the environment on which the application is running.

Why hosting environments?

As part of the software development life cycle, an application has to go through various phases like Development, Testing & Production. We as application developers need to configurable different parameters for different environments like each environment will have its own database, different versions (URL) of third party services, different application features like logging, bundling, minification, exception handling, etc based on the environment in which application is running.

Multiple environments let us control these parameters at runtime i.e database connections, third-party service URLs, application behavior like logging, exception, bundling, etc can be configured environment-wise.

ASP.NET Core 5 Design Patterns: Thinking code using architectural principles, testing, design patterns, and C

How to configure hosting environment in ASP.NET Core

There are a number of ways to configure hosting environment ASP.NET Core. This environment variable needs to be set on each machine on which the application is required to run.

using windows command line

To set the environment variable from the command prompt following command can be used. This is applicable for the current session and the app should be started using dotnet run command.

dotnet run --no-launch-profile
using PowerShell
dotnet run --no-launch-profile

Both command line & PowerShell sets environment variables only for the processes launched from that command window.

using windows environment variables

To configure hosting environment in ASP.NET Core variable globally in windows so that each you don’t need to set it before running the application use windows environment variables

To set environment variable globally open Control Panel ==> System ==> Advanced System Settings ==> Environment Variables ==> System Variable New/Edit & set values in ‘name’ and ‘value’ and click ok

configure hosting environment in ASP.NET Core
using web.config

Environment variable can be set in web.config as well. Environment variable can be specified for the process in the processPath attribute in web.config. Environment variables set in this section takes precedence over system environment variables.

<aspNetCore processPath="dotnet"
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />

Developers can use this to temporarily set the environment to development for debugging purpose.

using Per IIS Application Pool

Environment variable can also be set at IIS pool level for an application running in an isolated application pool. When ASPNETCORE_ENVIRONMENT is set for an app pool, its value overrides a setting at the system level.

   <add name="CorePool" managedRuntimeVersion="v4.0" managedPipelineMode="Classic">
         <add name="ASPNETCORE_ENVIRONMENT" value="Staging" />

AppCmd.exe can be used to add environment variables to the IIS pool level.

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='CorePool'].environmentVariables.[name='ASPNETCORE_ENVIRONMENT',value='Staging']" /commit:apphost

Restart IIS or Server after setting the environment variable in IIS for application to have new values available after changes.

How to use hosting environment in ASP.NET Core in your application

Environment based startup class

Below is the example of environment based startup class in which IWebHostingEnvironment has been injected to identify the environment and add code behavior accordingly.

public class Startup
    public Startup(IConfiguration configuration, IWebHostEnvironment env)
        Configuration = configuration;

        var builder = new ConfigurationBuilder()
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    //Remaining code was removed

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        if (env.IsDevelopment())


        //Remaining code was removed

In the above code in startup constructor environment variable name has been used to load appsettings.json file based on the environment configured for application i.e. appsettings.development.json or appsettings.production.json file will be used based on the environment set at runtime.

Also in the configure method different application behavior has been set for exception handling based on the environment set at runtime.

Newsletter Subscription

Stay updated! Instantly get notified about my new articles in your mailbox by subscribing via email

How to add custom hosting environment in ASP.NET Core

Development, Staging & Production environments are provided by default by the framework. But ASPNETCORE_ENVIRONMENT can be set to any string value. To configure your own environments following extension class can be added.

public static class HostingEnvironmentExtensions
    public const string QAEnvironment = "QA";
    public const string UATEnvironment = "UAT";
    public const string TestEnvironment = "Test";

    public static bool IsQA(this IWebHostEnvironment hostingEnvironment)
        return hostingEnvironment.IsEnvironment(QAEnvironment);

    public static bool IsUAT(this IWebHostEnvironment hostingEnvironment)
        return hostingEnvironment.IsEnvironment(UATEnvironment);

    public static bool IsTest(this IWebHostEnvironment hostingEnvironment)
        return hostingEnvironment.IsEnvironment(TestEnvironment);

Once configured it will be available under environment variable as shown below

ASP.NET Core Custom Environments


Hosting environment in ASP.NET Core allows developers to control how their application will behave in different environments. There are multiple ways available to set an environment variable for the application.

ASP.NET Core has provided 3 environments i.e. development, staging & production by default but custom environments can be added and practically any string can be used for environment name.


You can also check my other Article on ASP.NET Core bundling & minification:

Hope you found this article useful. Your support is appreciated!
Buy me a coffeeBuy me a coffee
Home 9 Programming 9 How to configure hosting environment in ASP.NET Core

Set start URL in ASP.NET Core – Quick & Easy ways

This article will cover the ways to set start URL in ASP.NET Core 5 applications i.e. change the default URL (http://localhost:5000) in ASP.NET Core applications. When you create any new ASP.NET Core application whether its MVC App or Web API and run it then it will...

Hangfire in ASP.NET Core – Easy way to Schedule Background Jobs

This article covers detail about how to integrate Hangfire in ASP.NET Core applications which is an easy way to schedule background jobs in .NET Core and .NET based applications. Background jobs or tasks allow the programmers to execute code on a different thread but...

How to Send Emails in ASP.NET Core – Quick & Easy Guide

These days sending emails like user email id confirmation, OTP emails, acknowledgments, etc. is a very common requirement of any application. In this article, we will see a demo about how to send emails in ASP.NET Core in quick & easy steps. We will create an...

ML.NET – Machine Learning with .NET Core – Beginner’s Guide

This article will get you started with the fundamentals of Machine Learning and how to get started with Machine Learning with .NET Core i.e. ML.NET. We will even learn different concepts of Machine learning with a brief overview. Introduction to Machine Learning...

Implement Cookie Authentication in ASP.NET Core – Detailed Guide

This article will get you started with implementing cookie authentication in ASP.NET Core applications. Cookie authentication allows you to have your own login/register screens & custom logic for user-id/password validation without the need to use ASP.NET Core...

ASP.NET Core Identity Roles based Authorization

This article will get you started with what are ASP.NET Core Identity roles and the need for roles, how to enable ASP.NET Core Identity roles in MVC Application, and how to implement role-based authorization. Role-based authorization is for basic authorization where...

Dependency Injection in ASP.NET Core 3.1 – Beginner’s Guide

This article will cover in details dependency injection in ASP.NET Core 3.1. ASP.NET Core is designed from the ground up to support the dependency injection design pattern. Dependency injection in ASP.NET Core provides a technique to create applications that are...

Real-time Web Applications with SignalR in ASP.NET Core 3.1

In this article, we will learn about real-time web & how to build real-time web applications using SignalR in ASP.NET Core. As part of this article, we will work on an application that can send real-time messages/alerts to all connected users or private messages...

Code Profiling using MiniProfiler in ASP.NET Core 3.1

This article will get you started with how to enable miniprofiler in ASP.NET Core to measure the performance of your application. MiniProlifer helps you understand how your application is performing by allowing you to profile any part of code in ASP.NET Core...

Microservices with ASP.NET Core 3.1

Microservices is a type of Architecture in which application is created as multiple small independent serviceable components. This article will cover in detail how to create microservices with ASP.NET Core, Serilog, Swagger UI, Health Checks & Docker containers....


Submit a Comment

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