ASP.NET核心:如何在特定环境中使用AppSetting

发布于 2025-02-11 18:32:13 字数 694 浏览 2 评论 0原文

在.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();
    }

I am adding a new builder as follows
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2025-02-18 18:32:13

在.NET 6中,您可以这样做。

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
    builder.Environment.EnvironmentName = "Linux";
    builder.Configuration.AddJsonFile("appSettings.Linux.json");
}

然后它将覆盖所有配置值。

AppSettings.linux.json

从配置中访问值

根据反馈更新答案。

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(config =>
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        config.AddJsonFile("appSettings.Linux.json", optional: true, reloadOnChange: true);
                    }
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });

in .NET 6, you can do like this.

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
    builder.Environment.EnvironmentName = "Linux";
    builder.Configuration.AddJsonFile("appSettings.Linux.json");
}

and then it will override all the configuration values.

appsettings.Linux.json
enter image description here

accessing value from Configuration
enter image description here

Updated answer based on feedback.

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration(config =>
                {
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        config.AddJsonFile("appSettings.Linux.json", optional: true, reloadOnChange: true);
                    }
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文