為每個環境提供 appsettings

對於每個環境,你需要建立單獨的 appsettings。{EnvironmentName} .json 檔案:

  • appsettings.Development.json
  • appsettings.Staging.json
  • appsettings.Production.json

然後開啟 project.json 檔案並將它們包含在 publishOptions 部分的 include 中。這列出了釋出時將包含的所有檔案和資料夾:

"publishOptions": {
  "include": [
    "appsettings.Development.json",
    "appsettings.Staging.json",
    "appsettings.Production.json"
    ... 
  ]
}

最後一步。在你的 Startup 類中新增:

.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

在建構函式中,你可以在其中設定配置源:

var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();