netcore使用IOptions

netcore 使用 IOptions 读取配置文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "HC": {
    "Redis": {
      "aa": "1"
    },
    "DB": {
      "bb": 2
    }
  }
}
appsetting
services.Configure<Test>(Configuration.GetSection("HC"));
ConfigureServices
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; }
    }
Test
public Test Test { get; set; }

        public api1Controller(IOptions<Test> options)
        {
            Test = options.Value;
        }
Controller
posted @ 2020-01-15 10:39  HeroCany  阅读(2017)  评论(0编辑  收藏  举报