How to setup ASPNETCORE_ENVIRONMENT variable on an IIS Website

How to setup ASPNETCORE_ENVIRONMENT variable on an IIS Website

When running an asp.net 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".

enter image description here

  • 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

enter image description here

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

enter image description here

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

enter image description here

  • 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 :-( :-( :-(

comments powered by Disqus