Dotnet Core 3.1:如何使用具有文件绝对路径的 AddJsonFile? [英] Dotnet Core 3.1: How to use AddJsonFile with absolute path to file?

查看:42
本文介绍了Dotnet Core 3.1:如何使用具有文件绝对路径的 AddJsonFile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dotnet app in which I need to pull in configuration from both relative paths (the regular appsettings.Json, appsettings.Development.json), and also, from the absolute path /config/appsettings.json. I can't find a way to do this. From the docs, the path arg is Path relative to the base path stored in Properties of builder. How can I set it up to read a config from absolute path /config/filename.json?

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

Motivation: I'm dockerizing the dotnet app, so I am planning to keep configuration that won't usually change between deployments in the regular publish/appsettings.json and deploy specific config in /config/appsettings.json (which is a volume I'll mount on the container at deploy time).

推荐答案

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

You can edit your CreateHostBuilder method like this:

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;
}

这篇关于Dotnet Core 3.1:如何使用具有文件绝对路径的 AddJsonFile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆