.NET 5.0 - 如何添加到现有配置?
.NET 5.0 - How do I add to an existing configuration?
这是我在 Startup.cs 中的启动构造函数:
private readonly IWebHostEnvironment _hostingEnv;
private IConfiguration Configuration { get; }
public Startup(IWebHostEnvironment env, IConfiguration configuration)
{
_hostingEnv = env;
Configuration = configuration;
}
我希望能够添加 Json 配置文件并构建配置,如下所示:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
但我想将其添加到 'configuration' 参数提供的现有配置中,而不是创建新配置。
我该怎么做?
我不知道这个 'configuration' 参数是从哪里传递给启动构造函数的。这是我的 Program.cs 文件
中的内容
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>()
.UseUrls(new[] { "http://0.0.0.0:9999" });
您可以像下面这样更改您的 Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
这是我在 Startup.cs 中的启动构造函数:
private readonly IWebHostEnvironment _hostingEnv;
private IConfiguration Configuration { get; }
public Startup(IWebHostEnvironment env, IConfiguration configuration)
{
_hostingEnv = env;
Configuration = configuration;
}
我希望能够添加 Json 配置文件并构建配置,如下所示:
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
但我想将其添加到 'configuration' 参数提供的现有配置中,而不是创建新配置。
我该怎么做?
我不知道这个 'configuration' 参数是从哪里传递给启动构造函数的。这是我的 Program.cs 文件
中的内容public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>()
.UseUrls(new[] { "http://0.0.0.0:9999" });
您可以像下面这样更改您的 Program.cs:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});