Net Core:调试时如何查找调用的Configuration Appsettings文件目录
Net Core: How to Find Configuration Appsettings File Directory being Called while Debugging
如何从这行代码中找到 Configuration (conf) 和 appsettings 文件目录?我可以用来查看其文件路径的 conf .属性 是什么?尝试调试,并在 Debug Watch window.
中查看其读取的配置文件
项目Startup.cs
public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
Configuration = configuration;
HostingEnvironment = hostingEnvironment;
}
public IHostingEnvironment HostingEnvironment { get; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var conf = Configuration;
IConfiguration appConf = conf.GetSection("ConnectionStrings");
var connstring = appConf.GetValue<string>("DatabaseConnection");
services.AddDbContext<DbContext>(a => a.UseSqlServer(connstring));
}
Appsettings.Json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DatabaseConnection": "Data Source=.;Initial Catalog=ApplicationDatabase;Integrated Security=True"
}
}
您可以尝试使用此代码从 appsettings.json 文件中获取连接字符串。
services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection")));
如何从这行代码中找到 Configuration (conf) 和 appsettings 文件目录?我可以用来查看其文件路径的 conf .属性 是什么?尝试调试,并在 Debug Watch window.
中查看其读取的配置文件项目Startup.cs
public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
Configuration = configuration;
HostingEnvironment = hostingEnvironment;
}
public IHostingEnvironment HostingEnvironment { get; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var conf = Configuration;
IConfiguration appConf = conf.GetSection("ConnectionStrings");
var connstring = appConf.GetValue<string>("DatabaseConnection");
services.AddDbContext<DbContext>(a => a.UseSqlServer(connstring));
}
Appsettings.Json
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DatabaseConnection": "Data Source=.;Initial Catalog=ApplicationDatabase;Integrated Security=True"
}
}
您可以尝试使用此代码从 appsettings.json 文件中获取连接字符串。
services.AddDbContext<DbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection")));