zoukankan      html  css  js  c++  java
  • 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();
            }

    前端显示效果:

  • 相关阅读:
    MySQL主从复制的作用?
    MySQL的逻辑架构
    SQL语句的执行流程
    Count(*)在不同引擎的实现方式
    视图
    MySQL经典练习题(五)
    pyinstaller基本操作
    git基本操作
    Ubuntu安装tensorflow
    ScrollView can host only one direct child
  • 原文地址:https://www.cnblogs.com/Linc2010/p/14316116.html
Copyright © 2011-2022 走看看