我想在基于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示例以包括此内容?
答案 0 :(得分:1)
“基于ASPNETCORE_ENVIRONMENT的特定于环境的appsettings.json” 已通过约定CreateDefaultBuilder()
在您的Program.cs
文件中完成。
因此您无需执行其他任何操作。只需设置您的ASPNETCORE_ENVIRONMENT
值