Asp.Net.Core 5 配置文件读取2

1,配置文件部分内容--appsettings.json

  "ConnectionStrings": {
    "ConnDefault": "123",
    "ConnList":[ "345", "2343", "0000" ]
  },

 

2,声明实体对象

  public class ConnectionStringsModel
    {
        public string ConnDefault { get; set; }
        public List<string> ConnList { get; set; }
    }

 

3,控制器构造函数-注入 IConfiguration

 private readonly ILogger<HomeController> _logger;
        private readonly IConfiguration _Configuration;

        public HomeController(ILogger<HomeController> logger,
            IConfiguration configuration

            )
        {
            _logger = logger;
            _Configuration = configuration;
        }

 

4,获取对象,并返回前端

获取对象:   var oValue = _Configuration.GetSection("ConnectionStrings").Get<Models.ConnectionStringsModel>();

 public IActionResult ReadConfig()
        {
            base.ViewBag.Version = _Configuration["Version"];
            base.ViewBag.AllowedHosts = _Configuration["AllowedHosts"];
            base.ViewBag.LoggingDefault = _Configuration["Logging:LogLevel:Default"];


            var oValue = _Configuration.GetSection("ConnectionStrings").Get<Models.ConnectionStringsModel>();

            base.ViewBag.ConnectionStrings = Newtonsoft.Json.JsonConvert.SerializeObject(oValue);


            return View();
        }

 

前端显示效果:

Asp.Net.Core 5 配置文件读取2

 

Asp.Net.Core 5 配置文件读取2

上一篇:Asp.Net.Core 5 脚本启动传递参数-并获取期参数值


下一篇:pytest生成html报告-使用pytest-html插件方式