简体   繁体   English

.Net Core 3.1 将额外的 config.json 文件添加到 Startup 中的配置参数

[英].Net Core 3.1 adding additional config.json file to configuration argument in Startup

I need to add another configuration file to my .Net Core Blazor project.我需要向我的 .Net Core Blazor 项目添加另一个配置文件。 Other sources (such as this ) mention using configuration builder in .Net core 2.2, but since in 3.1 the configuration is passed as a parameter into Startup, I have no way of appending another config onto it as far as I can tell.其他来源(例如this )提到在 .Net core 2.2 中使用配置构建器,但由于在 3.1 中,配置作为参数传递给 Startup,据我所知,我无法将另一个配置附加到它上面。 Maybe I'm looking at it wrong, but I'm assuming that the configuration passed in as a param has important configuration properties, therefor building it my self with just the additional config file seems like it leave those config properties out.也许我看错了,但我假设作为参数传入的配置具有重要的配置属性,因此仅使用附加配置文件自行构建它似乎将这些配置属性排除在外。

Ex: Doesn't work but should paint a picture of what I'm trying to do.例如:不起作用,但应该描绘出我正在尝试做的事情。

public Startup(IConfiguration configuration)
{
      Configuration = configuration;
      var builder = new ConfigurationBuilder(configuration)
              .AddJsonFile("accountconstants.json", optional: true, reloadOnChange: true);
      });
}

You can do it in Program.cs ie earlier in the pipeline rather than in Startup.cs .您可以在Program.cs执行此操作,即在管道中的较早位置而不是在Startup.cs

Example:例子:

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostContext, config) =>
            {
                var env = hostContext.HostingEnvironment;

                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

Your approach should work, but needs a little bit of tweaking.您的方法应该可行,但需要稍作调整。

The configuration that you create needs to be added to the DI Container.您创建的配置需要添加到 DI 容器。 The documentation for this is here .这方面的文档是here

I've achieved it as well in Azure Functions and other projects through the following:我也通过以下方式在 Azure Functions 和其他项目中实现了它:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IConfiguration>(provider => new ConfigurationBuilder()
            .AddEnvironmentVariables()
            .AddJsonFile("accountconstants.json", optional: true, reloadOnChange: true)
            .Build());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Debug的ASP.NET Core启动上找不到config.json - config.json Not Being Found on ASP.NET Core Startup in Debug 覆盖ASP.Net 5中Azure Web App中config.json文件中的配置值 - Overriding Configuration Values in config.json file in Azure Web App in ASP.Net 5 .NET Core:将 Config.json 等任意文件复制到项目的输出目录 - .NET Core: Copy Arbitrary file like Config.json to Project's Output Directory 在启动时使用 static 参数设置配置部分,而不是 .net 核心中的 json 文件 - Set configuration part in startup with static parameters not json file in .net core .NET控制台应用程序未读取config.json - .NET console app not reading config.json wwwroot中的vNext / .NET5 config.json - vNext / .NET5 config.json in wwwroot 如何从 ASP.NET5 中的 config.json 正确读取嵌套配置值? - How to properly read nested configuration values from config.json in ASP.NET5? 无法从.NET Core 2控制台应用程序中的config.json中读取数据 - Can't read data from config.json in .NET Core 2 console application ASP.NET Core 3.1 - 在启动中配置自定义 JSON - ASP.NET Core 3.1 - Configure custom JSON in Startup config.json - 在 ASP.NET vNext 中添加数据库连接字符串 - config.json - Adding database connection strings in ASP.NET vNext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM