.net core 讀取appsetting.json

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

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

  "Qny": {
    "qiniuyunAK": "AK", //ak
    "qiniuyunSK": "SK", //sk
    "qiniuyunBucket": "空間名稱", //存儲空間名稱
    "prefixPath": "http://upload.qiniup.com" //七牛雲地址
  }
}

2.建立一個Model app

    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中註冊服務this

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

4.在xxxcontroller中使用spa

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

5.打印輸出code

相關文章
相關標籤/搜索