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另存。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM