. NET core2.0 add a JSON file and convert it into a class injection controller using

Source: Internet
Author: User

In the previous article, we introduced how to read the JSON file from the definition, the data is read out, just deal with the time is too troublesome, need to write again and again, very boring. So is there a good way to do it? After studying, the way has been.

Since one reading is more troublesome, can you put it into a class? Of course you can. This kind of direct injection into this class, let the controller to read, then do not need to load it every time, once loaded can be used anywhere.

Since this article is an update to this previous article, please read the previous article before browsing this article

(1): Add Class classes to the Models folder and fill in the following code:

namespace coredemo.models{public    class class    {public        int classnum {get; set;}        public string ClassName {get; set;}        Public list<student> Students {get; set;}    }    public class Student    {public        string name {get; set;}        public int Age {get; set;}}    }

(2): Update Startup.cs script, update function configureservices:

public void Configureservices (iservicecollection services)        {            var classsetting = Jsonconvert.deserializeobject<class> (File.readalltext ("Jsonfile/class.json"));            Services. Addsingleton (classsetting);//Global Injection            services. Addmvc ();        }

(3): Update the home controller, inject the configuration class in and use:

public class Homecontroller:controller    {        private readonly class _classsetting;        Public HomeController (Class classsetting)        {            _classsetting = classsetting;        }        Public Iactionresult Index ()        {            return Content ("class number =" + _classsetting.classnum + ", class name =" + _ Classsetting.classname + "\ r"                + "Student name =" + _classsetting.students[0].name+ ", student age =" + _classsetting.students[0 ". Age            );        }    }

(4): Run the project, view the results of the operation, success:

Summary: If the configuration file one read is feasible, just too much trouble, if other places also need to load read again, waste development efficiency and operational efficiency. Loading and injecting directly into a class allows it to run at every location, with minimal cost and maximum efficiency.

(Reminder: If the error is found in the code because some assemblies do not have references, move the mouse directly to the error location and then you can reference the assembly to the project)

. NET core2.0 add a JSON file and convert it into a class injection controller using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.