How To Set Environment for an IIS Hosted .Net Core App

Mert Ilis
Nov 6, 2020

--

The environment for .Net Core applications can be determined via environment variables.

.Net Core applications use the “ASPNETCORE_ENVIRONMENT” value for determining the environment profile. This profile is very important and can be used to determine many application settings or for specific error pages and exception handling.

For example; you can provide different appsettings.json configuration per environment. You can have appsettings.Development.json for the development environment, appsettings.Staging.json for the staging environment, appsettings.Production.json for the production environment.

Another example; in the code, you can check the environment and add specific configuration per environment:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
}

For an IIS hosted ASPNet.Core application this environment can be set with the following command:

appcmd.exe set config -section:system.applicationHost/applicationPools /+”[name=’Core’].environmentVariables.[name=’ASPNETCORE_ENVIRONMENT’,value=’Production’]” /commit:apphost

You can run this command on the Command Prompt to set the environment to “Production” for the application pool “Core”. In order to apply this change for the running applications, we need to restart the webserver.

You can do this with the following commands:

net stop was /y
net start w3svc

Now the environment is set to “Production” and appsettings.Development.json will be ignored.

Hope it helps!

--

--

Mert Ilis

I’m a software development enthusiast who likes trying different web technologies and adding value to his team.