How to setup ASPNETCORE_ENVIRONMENT variable on an IIS Website

Posted on February 23rd, 2019

When running an core website on IIS, it will default to "Production", when ASPNETCORE_ENVIRONMENT is not set. This is actually a good default.

You could actually set the environment variable globally on the machine, but then all sites will be have the same value. So what if Staging and Production is on the same machine ...

You can actually set it on the website in IIS.

  • Open the "Internet Information Services (IIS) Manager.
  • Go to the Website where you want to set the environment variable.
  • Find the "Configuration Editor".

iis manager configuration editor

  • In the "Section" to the top left of the window, select system.webServer/aspNetCore in the dropdown and just to the right og this, REMEMBER to select ApplicationHost.config. If you fotget, you will set this variable in the used web.config for the running site. Then ofcause it will be overwritten when you deploy a new version of the website. The ApplicationHost.config sets it machine level, so the setting will live in a deffenrent place. You can read more about ApplicationHost.config here

iis manager configuration editor editapplication host config

  • Mark environmentVariables line and click the tree dots at the end to edit the list.

iis manager edit environment variables

  • Click the Add button and set the name to ASPNETCORE_ENVIRONMENT and value to Staging

iis manager add environment variable

  • Close the window and restart the website. The website should now have the ASPNETCORE_ENVIRONMENT variable set to Staging

Side note: Ofcause you should have different machines for each environment, but sometimes this is just not an option :-( :-( :-(