如何根据环境更改 appSettings.json 文件中的值

Muh*_*Ali 4 .net-core asp.net-core

我正在 .net core 项目中构建 API,并且必须在 appSettings.json 文件中存储 jwt 令牌的一些键值对。例如,我需要存储有效的发行者和有效的受众。为了进行开发,我有我的 appSettings.json 文件,如下所示:

`{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "jwt": {
    "JwtIssuer": "https://localhost:44332/",
    "JwtAudience": "https://localhost:44332/",
    "JwtSecretKey": "my super secure key"
  }
  "AllowedHosts": "*"
}`        
Run Code Online (Sandbox Code Playgroud)

对于开发环境来说,这很好,但是当我必须部署说 azure 时,我是否需要在 appSettings.json 文件中手动更改这些 URL,或者是否有任何其他有效的方法来管理这些根据环境自动更新的 URL。

Sae*_*deh 6

ConfigurationBuilder 的默认设置是查找appsettings.<EnvironmentName>.json文件,因此根据您正在使用的环境

当您使用 IIS Express 时,您处于开发环境;当您部署应用程序时,您的环境处于生产环境。这就是为什么你需要appsettings.Production.json