3

I look at several topics e.g. Automatically set appsettings.json for dev and release environments in asp.net core? and see that I can create appsettings.{Environment}.json in .NET Core apps and set the environment in launchSettings.json.

However, I am wondering what should I do to set this environment properly while publishing my app to IIS or Docker container. Before each publishing, should I change the environmentVariables parameter to Prod and then update it to Development after publish? I do not think so, but I could not find a suitable page explaining this. Could you please clarify me about this issue?

5
  • 1
    You need to set the environment variable on the respective machine and asp.net will read from the respective json based on environment variable Commented Feb 27, 2021 at 18:11
  • 1
    launchSettings.json is only used on the local development machine. Commented Feb 27, 2021 at 18:24
  • Thanks a lot both of you. Then as far as I understood, in the development machine, I can easily switch between PROD and DEV using launchSettings.json. But on the server machine, I should set the environment to PROD and then it automatically use the appsettings.Production.json file (respective json file). Is that all true? Commented Feb 27, 2021 at 18:29
  • 1
    @Maria yes, on the local machine you can switch environments using this file. Commented Feb 27, 2021 at 18:34
  • @GuruStron What about the other question related to the server machine? Commented Feb 27, 2021 at 18:37

1 Answer 1

3

First of all as docs state: launchSettings.json is only used on the local development machine.

As for using configuration file - by default next files are used:

  • appsettings.json using the JSON configuration provider.
  • appsettings.Environment.json using the JSON configuration provider. For example, appsettings.Production.json and appsettings.Development.json.

The environment for ASP.NET Core app determined next way:

To determine the runtime environment, ASP.NET Core reads from the following environment variables:

  • DOTNET_ENVIRONMENT
  • ASPNETCORE_ENVIRONMENT when ConfigureWebHostDefaults is called. The default ASP.NET Core web app templates call ConfigureWebHostDefaults. The ASPNETCORE_ENVIRONMENT value overrides DOTNET_ENVIRONMENT.

So the usual approach is to set ASPNETCORE_ENVIRONMENT environment variable to needed value on target host (machine, docker container, etc...)

Sign up to request clarification or add additional context in comments.

8 Comments

1. I think I can set the ASPNETCORE_ENVIRONMENT via cmd set ASPNETCORE_ENVIRONMENT=Production, etc. Is that true?
2. I have read using ASPNETCORE_ENVIRONMENT is better than DOTNET_ENVIRONMENT as it also supports Console apps. I am confused, which one should I prefer and why?
@Maria as written in the answer ASPNETCORE_ENVIRONMENT if present overrides DOTNET_ENVIRONMENT, so I use the former one in my projects. As for setting environment variable on windows see this answer.
@Maria also it seems that it can be set in IIS
Thanks a lot again for all your useful helps. I think I should use ASPNETCORE_ENVIRONMENT but not fully understand the difference exactly with DOTNET_ENVIRONMENT :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.