目 录CONTENT

文章目录

dotnetCore读取Appsettings中的配置内容支持对象集合

管理员
2024-08-22 / 0 评论 / 0 点赞 / 28 阅读 / 4716 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2024-08-22,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

.net Core的注入机制不过多赘述,直接进入正题

SystemConfig List集合的数据源

AppSettings 实体对象的数据源

1.首先在AppSettings.json文件中,录入自己的数据源。

{

  "Logging": {

    "LogLevel": {

      "Default": "Information",

      "Microsoft": "Warning",

      "Microsoft.Hosting.Lifetime": "Information"

    }

  },

  "SystemConfig": [

    {

      "Code": "10",

      "Name": "系统模块",

      "Sort": 10,

      "DefaultAction": "",

      "Children": [

        {

          "Code": "20",

          "Name": "系统页面",

          "Sort": 20,

          "DefaultAction": ""

        }

      ]

    },

    {

      "Code": "15",

      "Name": "自定义模块",

      "Sort": "15",

      "DefaultAction": "",

      "Children": [

        {

          "Code": "25",

          "Name": "自定义页面",

          "Sort": 25,

          "DefaultAction": ""

        }

      ]

    },

    {

      "Code": "500",

      "Name": "定制模块",

      "Sort": 500,

      "DefaultAction": "",

      "Children": [

        {

          "Code": "510",

          "Name": "定制新增",

          "Sort": 510,

          "DefaultAction": "/home/insert"

        },

        {

          "Code": "520",

          "Name": "定制编辑",

          "Sort": 520,

          "DefaultAction": "/home/update"

        }

      ]

    }

  ],

  "AllowedHosts": "*",

  "AppSettings": {

    "AccessKey": "111111",

    "SecretKey": "22222",

    "Bucket": "3333333",

    "Domain": "http://wwww.domain.com"

  }

}

2.在控制器中通过构造函数注入的方式,注入对象

此代码是实现List集合的方式,注释掉的地方,你可以根据你要实现的场景使用

 [Route("api/[controller]/[action]")]

    public class HomeController : Controller

    {

        private readonly IConfiguration _configuration;

        private AppSettings _appSettings;

        private List<SystemConfig> _systemConfig;

        public HomeController(IOptionsMonitor<List<SystemConfig>> systemConfig)

        {

        	//实现实体对象注入 

            //_appSettings = appSettings.CurrentValue;

  

			//实现List集合的注入

            _systemConfig = systemConfig.CurrentValue;

        }

        [HttpGet]

        public void Index()

        {

            var aa = _configuration["SystemConfig"];

        }

    }

3.定义我们的Module对象

public class SystemConfig

        {

            public String Code { get; set; }

            public String Name { get; set; }

            public String Sort { get; set; }

            public String DefaultAction { get; set; }

            public List<Children> Children { get; set; }

        }

        public class Children

        {

            public String Code { get; set; }

            public String Name { get; set; }

            public String Sort { get; set; }

            public String DefaultAction { get; set; }

        }

        public class AppSettings

        {

            public string AccessKey { get; set; }

            public string SecretKey { get; set; }

            public string Bucket { get; set; }

            public string Domain { get; set; }

        }

4.最关键的一步,在Startup.cs文件中,找到 ConfigureServices 方法,注册我们的对象集合即可

同理,注释掉的地方,是实现实体的地方

 //var appSettings = Configuration.GetSection("AppSettings");

            //services.Configure<AppSettings>(appSettings);

            var sysconfig = Configuration.GetSection("SystemConfig");

             services.Configure<List<SystemConfig>>(sysconfig);

做完这些步骤之后,Debug程序,因为我的Demo是API的方式,你可以直接通过访问路由,就可以拿到自己的实体对象

dotnetCoreGetAppSettings.7z

0

评论区