Example of using the Appsettings.json configuration file in the core Web (ASP)

Source: Internet
Author: User
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

Related Article

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.