1)先看丑陋的方法
读取 appsettings.json 然后在 Startup 的 ConfigureServices() 方法中进行注入:
public IConfigurationRoot Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IConfiguration>(Configuration); }
控制器中读取
public class HomeController : Controller
{
private readonly IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
_configuration = configuration;
}
读取到AppSettings下的子节点 的所有key
IConfigurationSection section = _configuration.GetSection("AppSettings");
List<string> list = new List<string>();
var childes = section.GetChildren();
foreach (var configSection in childes)
{
var conKey =configSection.Key;
}
其他需求可以参考http://www.cnblogs.com/dudu/p/6828045.html