netcore 使用 IOptions 读取配置文件

{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "HC": { "Redis": { "aa": "1" }, "DB": { "bb": 2 } } }

services.Configure<Test>(Configuration.GetSection("HC"));

public class Test { public Redis Redis { get; set; } public DB DB { get; set; } } public class Redis { public string aa { get; set; } } public class DB { public string bb { get; set; } }

public Test Test { get; set; } public api1Controller(IOptions<Test> options) { Test = options.Value; }