首页 > Web开发 > 详细

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

时间:2021-01-23 10:20:40      阅读:60      评论:0      收藏:0      [点我收藏+]

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

原文:https://www.cnblogs.com/Linc2010/p/14316116.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!