. NET core reads the configuration file in the class library Appsettings.json

Source: Internet
Author: User

In the. NET Framework era, our application configuration is generally written in the Web. config or app. config file, and reading the two configuration files requires only referencing the System.Configuration assembly, respectively.

system.configuration.configurationmanager.appsettings["SystemName"]; // Read appsettings configuration system.configuration.configurationmanager.connectionstrings["dbconnectionstr"]; // Read connectionstrings configuration

Read the configuration file and database links.

  

Now that we are finally in the. NET core era, more and more applications are being migrated to the. NET Core framework, and after the. NET core is released,. NET Core2.0 is more mature, and the original class library. NET core is basically all implemented in the. NET Framework box. And there are enhancements. So I'm also ready to join the. NET Core Army, so I've recently started practicing. NET core Dafa.

  

To cast a sword, must first iron, I have to step by step, read the configuration file is an application is essential, first understand how to read the configuration file,. NET The core configuration file is Appsettings.json, so I need to encapsulate the read Appsettings.json into the class library in order to be able to read the configuration in Appsettings.json easily in various classes. Read in startup does not say, in the class library to implement read how to play? Directly on the code, the Appsettings.json file contents are as follows:

{  "Logging": {    "Includescopes":false,    "LogLevel": {      "Default":"Warning"    }  },  "ConnectionStrings": {    "Cxyorder":"Server=laptop-aqul6mde\\mssqlservers;database=cxyorder; User Id=sa; password=123456; Trusted_connection=false;"  },  "Appsettings": {    "SystemName":the PDF. NET CORE",    "Date":"2017-07-23",    "Author":"PDF"  },  "serviceurl":"https://www.baidu.com/getnews"}

First, you need to introduce Microsoft.Extensions.Configuration to the class library project and Microsoft.Extensions.Configuration.Json package, load the Appsettings.json configuration file code in the class library as follows:

usingMicrosoft.Extensions.Configuration;usingMicrosoft.Extensions.Configuration.Json;namespacenetcoreorder.common{/// <summary>    ///reading configuration Files/// </summary>     Public classappconfigurtaionservices { Public StaticIConfiguration Configuration {Get;Set; } Staticappconfigurtaionservices () {//Reloadonchange = True when Appsettings.json is modified reload            Configuration=NewConfigurationbuilder (). ADD (NewJsonconfigurationsource {Path ="Appsettings.json", Reloadonchange =true}).                    Build (); }    }}

Use the following method to read the database link string

AppConfigurtaionServices.Configuration.GetConnectionString ("cxyorder"/ / get Server=laptop-aqul6mde\\mssqlservers;database=cxyorder; User Id=sa; password=123456; Trusted_connection=false;

Read first-level configuration node configuration

appconfigurtaionservices.configuration["serviceurl"]; //  https://www.baidu.com/getnews

Read level two child node configuration

appconfigurtaionservices.configuration["appsettings:systemname"]; // get PDF. NET CORE appconfigurtaionservices.configuration["appsettings:author"]; // Get PDF

Note that if the FileNotFoundException exception is thrown in the Appconfigurtaionservices class, If the Appsettings.json file is not found in the description directory, on the project Appsettings.json file, right--Properties--Change the value of the Copy to output directory entry to always copy.

Side notes, if anything wrong, look!

. NET core reads the configuration file in the class library Appsettings.json

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.