This article mainly introduces the method of using the Appsettings.json configuration file in the ASP. NET core Web, the detailed example code is given, the friend who needs can refer to the study, let's take a look at it.
Objective
Recently, after studying the porting of the ASP. NET core to Linux, it was just a learning process.
Porting code is basically smooth, but found that. NET core does not have the ConfigurationManager, unable to read and write the configuration file, write a separate XML, such as the trouble, on Google, found a method, then recorded as follows, convenient for later search:
Methods are as follows
Configuration file Structure
public class Demosettings{public string Maindomain {get, set;} public string SiteName {get; set;}}
Display effect in Appsettings.json
Appsettings.json
{"Demosettings": {"Maindomain": "Http://www.mysite.com", "SiteName": "My Main Site"}, "Logging": {"includescopes": FAL SE, "LogLevel": { "Default": "Debug", " System": "Information", "Microsoft": "Information"}}}
Configure Services
Original Configuration
public void Configureservices (iservicecollection services) {//ADD framework services. Services. Addmvc ();}
Custom
public void Configureservices (iservicecollection services) {//ADD framework services. Services. Addmvc (); Added-uses ioptions<t> for your settings. Services. AddOptions (); Added-confirms that we had a home for our demosettings services. Configure<demosettings> (Configuration.getsection ("demosettings"));}
And then inject the settings into the appropriate controller to use the
public class homecontroller:controller{Private demosettings configsettings {get; set;} Public HomeController (ioptions<demosettings> settings) { configsettings = settings. Value; Public Iactionresult Index () { viewdata["SiteName"] = Configsettings.sitename; return View (); }}
summary