.Net Core 读取配置文件主要是通过IConfiguration,是.Net Core自动注入的,默认构造函数注入方式。
1、Program控制台启动中添加Json配置文件
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(c =>
{
c.AddJsonFile("appsetting.json", false, true);
})
.UseStartup<Startup>();
}
2、在Controller中调用
private IConfiguration Configuration;
public TestController(IConfiguration configuration)
{
Configuration = configuration;
}
{
"Service": {
"TestUrl": "http://127.0.0.1:9500",
}
}
读取配置节点内容,直接Configuration["
Service:
IdentityServerUrl"];