.net core 3.0 appsettings.json 每个环境

.net core 3.0 appsettings.json per environment

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

微软为startup.cs给出的例子对于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 示例以包含此内容?

"environment specific appsettings.json based on ASPNETCORE_ENVIRONMENT" 已经按照惯例通过 Program.cs 文件中的调用 CreateDefaultBuilder() 完成。

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

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