.NET 7.0 Program.cs访问appsettings.json配置文件
//在.Net Core中,读取配对文件是通过IConfiguration接口操作的,实例化接口对象如下所示:
Program.cs中配置如下:
//引用Microsoft.Extensions.Configuration;
var Configuration = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.test.json", true, reloadOnChange: true).Build();
或者如下:
IConfiguration Configuration = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile("appsettings.test.json", true, reloadOnChange: true).Build();
Console.WriteLine($"Logging:{Configuration["Logging:LogLevel:Default"]}"); //测试读取