ASP.NET核心:如何在特定环境中使用AppSetting
在.NET 6.0 WebAPI应用程序中,
我有以下两个文件: AppSettings.json AppSettings.linux.json
我希望该应用程序在Linux环境中运行时使用AppSettings.linux.json。以下是我到目前为止尝试过的代码,但不幸的是它无法正常工作。在Linux环境中运行时,它仍在访问AppSettings.json文件。
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.AddJsonFile("appSettings.Linux.json" , optional: true, reloadOnChange: true);
builder.Build();
}
In .NET 6.0 WebApi application
I have the following two files:
appSettings.json
appSettings.Linux.json
I want the application to use appSettings.Linux.json when running in linux environment. The following is the code that I have tried so far but unfortunately it is not working. It is still accessing appSettings.json file while running in linux environment.
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
IConfigurationBuilder builder = new ConfigurationBuilder()
.AddJsonFile("appSettings.Linux.json" , optional: true, reloadOnChange: true);
builder.Build();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在.NET 6中,您可以这样做。
然后它将覆盖所有配置值。
AppSettings.linux.json

从配置中访问值

根据反馈更新答案。
in .NET 6, you can do like this.
and then it will override all the configuration values.
appsettings.Linux.json

accessing value from Configuration

Updated answer based on feedback.