zoukankan      html  css  js  c++  java
  • 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另存。

  • 相关阅读:
    r.js合并实践 --项目中用到require.js做生产时模块开发 r.js build.js配置详解
    javascript模块化编程 从入门到实战
    gulp、browsersync代理跨域
    TensorFlow 1.4利用Keras+Estimator API进行训练和预测
    python multiprocess pool模块报错pickling error
    python中用修饰器进行异常日志记录
    利用Laplacian变换进行图像模糊检测
    Keras查看model weights .h5 文件的内容
    python中利用redis构建任务队列(queue)
    Tensorflow 使用slim框架下的分类模型进行分类
  • 原文地址:https://www.cnblogs.com/hanke123/p/13722741.html
Copyright © 2011-2022 走看看