zoukankan      html  css  js  c++  java
  • .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.打印输出

  • 相关阅读:
    脚本添加crontab任务
    docker mysql8 注意
    使用 logrotate 清理日志
    腾讯云cos对象在线显示
    快速部署私人git服务--基于docker化Gogs
    grep 使用
    vsftpd 新增虚拟用户
    unistd.h
    ffmpeg
    H264视频压缩算法
  • 原文地址:https://www.cnblogs.com/mi21/p/10907948.html
Copyright © 2011-2022 走看看