net core读取配置(xxx.json)封装成model 依赖注入

例子:这里有个学生的信息,我想在整个项目中都可以直接调用他的基础信息。

1.appsetting.json文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "StudentModel": {
    "Name": "张三",
    "Sex": "",
    "Class": "三年级一班",
    "Age": 22,
    "BodyLong": 180,
    "HomeDress": "脑残省,脑残市,脑残区,脑残花园201",
    "Flag": true
  }
}

2.新建Studentmodel 名称一定对应上

  public class StudentModel
    {
        public string Name { get; set; }
        public string Sex { get; set; }
        public string Class { get; set; }
        public int Age { get; set; }
        public int BodyLong { get; set; }
        public string HomeDress { get; set; }
        public bool Flag { get; set; }
    }

3.Startup.cs 文件中获取下配置文件并转成对象,注入到服务中。

  public void ConfigureServices(IServiceCollection services)
        {
            StudentModel studentModel = Configuration.GetSection("StudentModel").Get<StudentModel>();//根据节点的父级的名称获取子节点
            services.AddSingleton(studentModel);
            services.AddControllersWithViews();
        }

4.1controller中注入下 _studentModel 可以直接使用了

  public readonly StudentModel _studentModel;
        public HomeController(StudentModel studentModel)
        {
            _studentModel = studentModel;
        }

        public IActionResult Index()
        {
            var student = _studentModel;
            return View(student);
        }

4.2index界面的使用

@{
    ViewData["Title"] = "Home Page";
}
@model StudentModel
@inject StudentModel studentModel
<div class="text-center">
    
    <h1 class="display-4" style="color:white; background-color:black;text-align:left;">界面注入</h1>
    <h2 style="text-align:left;">姓名:@studentModel.Name</h2>
    <h2 style="text-align:left;">性别:@studentModel.Sex</h2>
    <h2 style="text-align:left;">班级:@studentModel.Class</h2>
    <h2 style="text-align:left;">年龄:@studentModel.Age</h2>
    <h2 style="text-align:left;">身高:@studentModel.BodyLong</h2>
    <h2 style="text-align:left;">
        地址:@studentModel.HomeDress
    </h2>
    <h1 class="display-4" style="color:white; background-color:black; text-align:left;">后台传递</h1>
    <h2 style="text-align:left;">姓名:@Model.Name</h2>
    <h2 style="text-align:left;">性别:@Model.Sex</h2>
    <h2 style="text-align:left;">班级:@Model.Class</h2>
    <h2 style="text-align:left;">年龄:@Model.Age</h2>
    <h2 style="text-align:left;">身高:@Model.BodyLong</h2>
    <h2 style="text-align:left;">
        地址:@Model.HomeDress
    </h2>
</div>

效果:

 

 

 延伸下:

  1.假设配置文件,存在二级节点

  "StudentOne": {
    "Name": "张三",
    "Sex": "男",
    "Teacher": {
      "Name": "老师01",
       "Sex": "女"
    }

  }

2.封装俩model

  public class StudentOne
    {
        public string Name { get; set; }
        public string Sex { get; set; }

        public Teacher Teacher { get; set; }
    }


 public class Teacher
    {
        public string Name { get; set; }
        public string Sex { get; set; }
    }

剩下的操作同上

备注:

获取出的汉字出现乱码。这是因为json文件的编码方式不对。记事本打开下,选择utf-8另存。

 

posted @ 2020-09-24 10:12  人走茶亦凉  阅读(640)  评论(0编辑  收藏  举报