c# - 点网核心 3.1 : How to use AddJsonFile with absolute path to file?

标签 c# .net asp.net-web-api .net-core

我有一个 dotnet 应用程序,我需要从两个相对路径(常规 appsettings.Jsonappsettings.Development.json)提取配置,并且,来自绝对路径 /config/appsettings.json。我找不到办法做到这一点。来自 the docs ,路径 arg 是 相对于构建器属性中存储的基本路径的路径。 我如何设置它以从绝对路径 /config/filename.json 读取配置?

动机: 我正在对 dotnet 应用程序进行 docker 化,因此我计划保留通常不会在常规 publish/appsettings.json 中的部署之间更改的配置,并将特定配置部署在/config/appsettings.json(这是我将在部署时装载到容器上的卷)。

最佳答案

您可以像这样编辑您的 CreateHostBuilder 方法:

public static IHostBuilder CreateHostBuilder(string[] args)
{
    var builder = Host.CreateDefaultBuilder(args);
    builder.ConfigureAppConfiguration(cfgBuilder =>
       {
           var provider = new PhysicalFileProvider("/config"); // Create a dedicated FileProvider based on the /config directory
           cfgBuilder.AddJsonFile(provider, "filename.json", true, true); // Add the filename.json configuration file stored in the /config directory
       });
    builder.ConfigureWebHostDefaults(webBuilder =>
       {
            webBuilder.UseStartup<Startup>();
       });

    return builder;
}

关于c# - 点网核心 3.1 : How to use AddJsonFile with absolute path to file?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63170073/

相关文章:

c# - 如何在 .NET 中以编程方式更改默认应用程序池设置属性

c# - System.Net.Sockets.SocketException 灾难

azure - PowerShell 脚本无法执行。有关详细信息,请参阅输出窗口

http-post - 如何使用 TwiML 在 C# 中读取 Twilio POST 参数?

c# - new Thread(void Target()) 和 new Thread(new ThreadStart(void Target())) 有什么区别?

c# - 当我使用 Color.FromArgb() 创建颜色时,为什么 Color.IsNamedColor 不起作用?

c# - 如何使用 C# 4.0 读取签名请求

.net - AppFabric - Etw - 无法注销跟踪提供程序

.net - System.Diagnostics.Process.Start() 从 WCF 服务运行时拒绝访问

asp.net - MultipartStreamProvider 在异步工作后丢失 HttpContext