how to control the appsettings.json deployment based on environment in .Net Core

Viewed 968

I have ASP.Net Core 3.1 application & it has environment based appsettings.json as

  • appSettings.UAT.json
  • appSettings.Dev.json
  • appSettings.Prod.json

When the app is published/deployed to any env say UAT, all the appsettings.json gets deployed to the UAT machine.

The requirement is that if the machine/env is UAT, then only below appSettings should be deployed

  • appSettings.json
  • appSettings.UAT.json

& if the env is Prod then

  • appSettings.json
  • appSettings.Prod.json

I know the other way is to store/fetch configuration from Environment variables rather appSettings.json but for now that has extra cost for me.

Thanks!

2 Answers

You can set up the "ASPNETCORE_ENVIRONMENT" variable against your 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".
  • In the "Section" part of Configuration Editor, select system.webServer/aspNetCore in the left dropdown select ApplicationHost.config.
  • Click on environmentVariables then you will get the Current env variable. Add a new env variable.
  • name will be ASPNETCORE_ENVIRONMENT and value will be Development[staging/Prod]. In your case it will be UAT or DEV or Prod
  • Close the window and restart the website.

Give a try with the above.

Related