netcore使用IOptions

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

startup.cs

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;
        }

来源  :https://www.cnblogs.com/herocany/p/netcore_ioptions.html

原文地址:https://www.cnblogs.com/shy1766IT/p/13267027.html