asp.net-core-2.0 - asp.net core 2.0 特定版本 appsettings
问题描述
我正在将 apt.net core 2.0 web api 部署为 Windows 服务。我的每个环境都有一个 appsettings 文件(例如:appsettings.Development.json)。
但是,我的环境特定 appsetting 文件未加载到我的配置中。这是代码:
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
var pathToContentRoot = Path.GetDirectoryName(pathToExe);
return WebHost.CreateDefaultBuilder(args)
.UseContentRoot(pathToContentRoot)
.ConfigureAppConfiguration((builderContext, config) =>
{
IHostingEnvironment env = builderContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
//.AddJsonFile("appsettings.Development.json", optional: true, reloadOnChange: true)
})
.UseStartup<Startup>()
.UseUrls(myUrl)
.Build();
我试图通过硬编码(注释中的代码)值来加载特定于环境的配置文件,这是有效的。我还可以保证我的环境名称和文件名之间没有拼写错误。
我的环境名称是通过 dployment 机器上的环境变量设置的。
解决方案
第二个 .addJson 应该是
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
您在字符串之前缺少 $
推荐阅读
- python-2.7 - 为什么在使用 aws 胶水将 csv 转换为 parquet 时创建了多个文件?
- amazon-web-services - Lambda 无法访问互联网
- javascript - 如何访问通过函数传回的承诺?
- python - python中的错误排列
- java - 如何解决 - 控制台错误日志无法写入 Log4j2 配置文件
- javascript - 根据对象数组对字符串数组进行排序
- r - R:2019 年更改后的网络抓取 yahoo.finance
- javascript - ChartJS - 饼图的标签太长并隐藏饼图
- ios - 如何确保两个屏幕只支持纵向模式?
- r - 尝试使用 gl_model 函数时,数据框具有不同的维度