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

 

前端显示效果:

 

posted @ 2021-01-23 00:29  Lin_pin  阅读(20)  评论(0编辑  收藏