.net核心3.0每个环境的appsettings.json

时间:2019-11-05 16:55:27

标签: .net-core .net-core-3.0 asp.net-core-3.0

我想在基于appsettings.json的特定环境ASPNETCORE_ENVIRONMENT中阅读。

Microsoft为core.3.0提供的示例如下:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }

在以前的版本中,我看到的添加特定于环境的appsettings.json的代码是:

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

        Configuration = builder.Build();
    }

我如何更改标准的Microsoft示例以包括此内容?

1 个答案:

答案 0 :(得分:1)

“基于ASPNETCORE_ENVIRONMENT的特定于环境的appsettings.json” 已通过约定CreateDefaultBuilder()在您的Program.cs文件中完成。

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.0#default-configuration

因此您无需执行其他任何操作。只需设置您的ASPNETCORE_ENVIRONMENT