Before publishing a service or application to the production, we all may want to see how it behaves on test environments. So we have test servers or test dbs to check apis behaviours with this test environments.

Today I m going to show you how you publish your ASPNET Core Web API to the specific environment that will use specific appsettings.json file.

As you know there are appsettings.json that stores your connection strings, or other setttings. You can create various appsettings.json for your various environments. If you have test environment you can set appsettings.Development.json or for your staging environment you can set appsettings.Staging.json and for you productions settigs you can put them into the appsetting.json. But how do your environments know which appsettings.json the will use?

There are many methods you will see when you google it. I tried many of them but the best and easiest solution is to set it from IIS Settings. Let’s see how it can be done.

Open IIS and choose you web site from Sites folder.

IIS Settings_1

There is Configuration Editor that we will add our Name-Value configuration.

IIS Settings_2

In Configuration Editor there is Section and From comboboxes.

IIS Settings_3

Section will be system.webServer/aspNetCore From will be ApplicationHost.config

There is environmentVariables part where we will add our environment variables depends on our appsettings.json.

Right click and choose ‘environmentVariables’ Element and choose **Edit Items

IIS Settings_4

On the right side there is Add button, click it and enter name as ‘ASPNETCORE_ENVIRONMENT’ and value is if you want to set development, enter Development or if you want this machine run Staging put value as ‘Staging’. Thats all. When you run the Web API it will use the appsettings.json related to the value that you set as variable.

IIS Settings_5

Thanks for reading. See you soon 😄