.net core 读取appsetting.json(一):使用实体取值

1.在appsetting.json 文件中添加自定义配置

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",

  "Qny": {
    "qiniuyunAK": "AK", //ak
    "qiniuyunSK": "SK", //sk
    "qiniuyunBucket": "空间名称", //存储空间名称
    "prefixPath": "http://upload.qiniup.com" //七牛云地址
  }
}

2.创建一个Model 

    public class QnySetting
    {
        public string qiniuyunAK { get; set; }
        public string qiniuyunSK { get; set; }
        public string qiniuyunBucket { get; set; }
        public string prefixPath { get; set; }
    }

3.在Startup.cs中注册服务

 services.Configure<QnySetting>(this.Configuration.GetSection("Qny"));

4.在xxxcontroller中使用

     private readonly QnySetting _Quy;
        public UploadController(IOptions<QnySetting> Quy)
        {
            _Quy = Quy.Value;
        }
        public IActionResult Index()
        {
            Console.WriteLine(_Quy);
            return View();
        }

5.打印输出

posted @ 2019-05-22 19:13  sunshuaize  阅读(1833)  评论(0编辑  收藏  举报