我有一个 dotnet 应用程序,我需要从两个相对路径(常规 appsettings.Json
、appsettings.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/