ASP.NET Core(.NET 6)程序中如何在其他类中访问Configuration配置文件信息呢?
1.7K 次浏览
ASP.NET Core的应用程序中,在Startup.cs
启动文件中,我们可以比较容易地访问配置文件信息,比如:
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
app.UseMvc();
}
}
但如何在其他类文件(非控制器)中访问程序的配置文件appsettings.json
中的配置信息呢?