How to deal with environmental differences when deploying an asp.net core application?

Is there a way to change the environment settings when deploying an ASP.NET Core application (for example, converting configuration files using the debug / release assembly)?

What would be the best approach to support multiple environment settings in .NET Core applications (something similar to <appSettings file="local.config"> for local, intermediate, and production)?

+6
asp.net-core asp.net-core-mvc
source share
1 answer

The central configuration file is appsettings.json , and you can have several files, such as appsettings.Production.json , etc. that will be downloaded and override the settings from appsettings.json .

for example

  // Set up configuration sources. var builder = new ConfigurationBuilder() .SetBasePath(hostEnv.ContentRootPath) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); 

All you need for this is an environment variable to set the environment type (see the documentation here ).

You can also override environment variables if you add AddEnvironmentVariables() to your configuration constructor. Therefore, if you have appsettings.json

 { "Data" { "Default" { "ConnectionString" : "..." } } } 

and you want to override this through an environment variable, you have to set up an environment variable called "Data: Default: ConnectionString" and this value will override the parameters in appsettings.config and appsettings.Production.config with the value from the environment variable.

You can find more details in the official documentation here .

+11
source share

All Articles