其他分享
首页 > 其他分享> > .net core 添加配置文件

.net core 添加配置文件

作者:互联网

1 在Startup中添加

        public Startup(IWebHostEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }

2 在 Program 中

.net 5

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseConfiguration(
                        new ConfigurationBuilder()
                        .SetBasePath(System.IO.Directory.GetCurrentDirectory())
                        .AddJsonFile(path: "ocelot_settins.json", optional: true, reloadOnChange: true)
                        .Build()
                     );
                    webBuilder.UseStartup<Startup>();
                });

3.net 6 

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFile("configuration.json", optional: false, reloadOnChange: true);

标签:core,reloadOnChange,配置文件,optional,builder,json,AddJsonFile,net,true
来源: https://www.cnblogs.com/tomorrow0/p/16337247.html