The data configuration and content in the. Netcore uses a JSON file instead of an XML file from the previous framework, so how do you get the data in JSON? Here's how to get the JSON file in. NET Core.
First, create a new. NET core Web application, create a new folder Jsonfile, and create a new Class.json file in the Jsonfile folder.
Right-Class.json, set assignment to output directory to: Always copy (very important).
Then add the content in the Class.json:
{ "Classnum":"1", "ClassName":"high One by one classes", "Students": [ { "name":"Obama", " Age": - }, { "name":"Putin", " Age": - } ]}
Right-click Project dependencies: Manage Guget packages, add Microsoft.Extensions.Configuration assemblies to your project
Finally, add the relevant code to get the JSON file data in the home controller:
namespace coredemo.controllers{public class Homecontroller:controller {public iactionresult Index () { var builder = new Configurationbuilder (). Addjsonfile ("Jsonfile/class.json"); var config = builder. Build (); String str = "class:" + config["Classnum"]+ "/"; Return Content ("class number =" +config["Classnum"]+ ", class name =" + config["ClassName"]+ "\ r" + "Student name =" +config["students:0: Name "]+", student age = "+ config[" Students:0:age "] );}}}
Run the project, display the results of the run, the data has been read out and displayed in the browser, indicating that the ability to get the JSON file configuration content has been implemented:
. Netcore getting JSON file configuration content