为每个环境提供 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();