How can I add a custom JSON file into IConfiguration?

后端 未结 2 1344
北海茫月
北海茫月 2020-12-17 16:27

I\'m using asp.net + Autofac.

I\'m trying to load a custom JSON configuration file, and either create/instance an IConfiguration instance based on that, or at least

2条回答
  •  孤城傲影
    2020-12-17 17:13

    For .Net Core 2.2, you need to modify Program.cs:

    Before

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
               .UseStartup();
    

    After

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
    
                //This is how you attach additional JSON files
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    config.AddJsonFile("customSettings.json", optional: false, reloadOnChange: false);
                })
                //End of update
                .UseStartup();
    

    For the latest amendments and to add other kinds of custom settings, please refer to Microsoft documentation at the following article.

提交回复
热议问题