暂存环境没有采用正确的连接字符串 Asp.Net Core

Staging environment does not take the correct connection string Asp.Net Core

暂存环境没有采用正确的连接字符串。

我有三个环境。开发、登台和发布。 我的应用程序设置如下所示:

{
 "Data": {
  "DefaultConnection": {
  "ConnectionString": "My connection string"
}
},
 "Logging": {
  "IncludeScopes": false,
  "LogLevel": {
  "Default": "Verbose",
  "System": "Information",
  "Microsoft": "Information"
  }
 }
}

我还有两个名为 appsettings.Staging.json 和 appsettings.Release.json 的文件。 在项目属性下,我制作了两个配置文件:Staging & Release 都有一个 Hosting: E​​nvironment,其中 Value Staging in staging 和 Release in Release。它在 VS 中运行良好,但是当我将它上传到 Azure 时,它​​总是采用我的本地主机连接字符串而不是我的在线数据库的默认应用程序设置。 我究竟做错了什么?

如果文本文件中包含敏感数据,则不应存储连接字符串,并且应直接在 Azure 中的 AppSettings 中设置值以进行生产(或查看类似 Key Vault) to set the value. I would recommend reading https://docs.asp.net/en/latest/security/app-secrets.html and https://docs.asp.net/en/latest/fundamentals/configuration.html.

的内容)

要解决从正确的设置文件中读取值的问题,请参见下文。

您需要在 Azure 中设置环境值。在 Azure 门户中,在您的 WebApp 上转到 All Settings->Application Settings->AppSettings 并添加设置 ASPNET_ENVHosting:Environment 以及 Staging 的值(或您感兴趣的任何其他环境。

要读取 appsettings.Staging.json 文件,您需要执行以下操作: