.net core 控制器中获取appsettings.json中的配置项

创建与配置相同的实体类

1、单个实体对象

appsettings.json 中配置代码

 "AppSetting": {
    "SSOPassport": "http://localhost:1908", //webapi接口地址
    "Version": "1.0" //如果为demo,则屏蔽Post请求
  }

实体类对象代码

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Rextec.SOA.MVC.Models
{
    public  class AppSetting
    {
        /// 
        /// SSO地址
        /// 
        public string SSOPassport { get; set; }

        /// 
        /// 版本信息
        /// 如果为demo,则屏蔽Post请求
        /// 
        public  string Version { get; set; }

        public AppSetting(IConfiguration config)
        {
            SSOPassport = config["AppSetting:SSOPassport"];
            Version = config["AppSetting:Version"];
        }
    }
}

2、Json数组对象

appsettings.json 中配置代码

 "Menus": [
    {
      "MenuItem": {
        "MenuName": "模块管理",
        "Url": "/",
        "IconName": "layui-icon-app",
        "SortNo": 1
      },
      "Children": [
        {
          "MenuName": "应用管理",
          "Url": "/SystemManager/ApplicationList",
          "IconName": "layui-icon-website",
          "SortNo": 1
        },
        {
          "MenuName": "菜单管理",
          "Url": "/SystemManager/MenuList",
          "IconName": "layui-icon-template-1",
          "SortNo": 2
        }
      ]
    },
    {
      "MenuItem": {
        "MenuName": "系统管理",
        "Url": "/",
        "IconName": "layui-icon-set",
        "SortNo": 2
      },
      "Children": [
        {
          "MenuName": "组织机构管理",
          "Url": "/SystemManager/OrganizationList",
          "IconName": "layui-icon-group",
          "SortNo": 1
        },
        {
          "MenuName": "角色管理",
          "Url": "/SystemManager/RoleList",
          "IconName": "layui-icon-component",
          "SortNo": 2
        },
        {
          "MenuName": "人员管理",
          "Url": "/SystemManager/UserList",
          "IconName": "layui-icon-user",
          "SortNo": 3
        }
      ]
    }
  ]

实体类对象代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace Rextec.SOA.MVC.Models
{
    /// 
    /// 菜单
    /// 
    public class Menus
    {
        public List MenusInfos { get; set; }
        public Menus(IOptions> options)
        {
            MenusInfos = options.Value;
        }
    }
    /// 
    /// 菜单信息
    /// 
    public class MenuItem
    {
        /// 
        /// 菜单名称
        /// 
        public string MenuName { get; set; }
        /// 
        /// 请求路径
        /// 
        public string Url { get; set; }
        /// 
        /// 菜单排序
        /// 
        public int SortNo { get; set; }
        /// 
        /// 菜单图标
        /// 
        public string IconName { get; set; }
    }
    public class MenusInfo
    {
        /// 
        /// 父级菜单
        /// 
        public MenuItem MenuItem { get; set; }
        /// 
        /// 二级菜单
        /// 
        public List Children { get; set; }
    }
}

3、startup中相关代码

            services.AddSingleton();
            services.AddOptions();
            services.Configure>(Configuration.GetSection("Menus"));

4、控制器获取数据实现

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Rextec.SOA.MVC.Models;
using Rextec.SOA.Infrastructure;

namespace Rextec.SOA.MVC.Controllers
{
    public class HomeController : Controller
    {
        private readonly IConfiguration configuration;
        private readonly IOptions> menusInfos;
        public HomeController(IConfiguration _configuration, IOptions> _menusInfos)
        {
            configuration = _configuration;
            menusInfos = _menusInfos;
        }
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Main()
        {
            return View();
        }
        public ActionResult Git()
        {
            return View();
        }
        /// 
        /// 获取apiUrl
        /// 
        /// 
        [HttpGet]
        public string GetWebApiUrl()
        {
            string apiUrl= configuration.GetSection("AppSetting:SSOPassport").Value;
            return apiUrl;
        }
        /// 
        /// 加载菜单
        /// 
        /// 
        [HttpGet]
        public string GetMenu()
        {
            var menus = menusInfos.Value;
            return JsonHelper.Instance.Serialize(menus);
        }
    }
}

 

你可能感兴趣的:(.Net,Core,C#)