且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 .NET Core 中将应用设置与环境变量合并

更新时间:2023-02-16 09:52:22

config.
    AddJsonFile("appsettings.json").
    AddJsonFile("appsettings.docker.json", true).
    AddEnvironmentVariables();

实际上足以使用环境变量覆盖 appsettings 值.

is actually enough to override appsettings values using environment variables.

假设您的 appsettings.json 文件中有以下内容;

Let's say you have the following in your appsettings.json file;

{
  "Logging": {
      "Level": "Debug"
  }
}

您可以通过将名为 Logging:Level 的环境变量设置为您的首选项值来覆盖 Logging.Level 的值.

you can override value of Logging.Level by setting the environment variable named Logging:Level to the value of your preference.

请注意 : 用于指定环境变量键中的嵌套属性.

Be aware that : is used to specify nested properties in environment variable keys.

另请注意:来自 文档;

如果您系统上的环境变量名称中不能使用冒号 (:),请将冒号 (:) 替换为双下划线 (__).