A read profile in the. NET Core class library was previously written, with limited knowledge of the. NET core read configuration file, which is supplemented here:
The contents of the configuration file are as follows:
{ " : " homeinns.com , " person " name ": " xfh , " : 26
Reference Microsoft.Extensions.Configuration.Json
Package
usingSystem;usingSystem.Configuration;usingSystem.IO;usingMicrosoft.Extensions.Configuration;namespacecoreapp{classProgram {/// <summary> ///load configuration file, build Iconfigurationroot/// </summary> Private Static ReadOnlyIconfigurationbuilder Configurationbuilder =NewConfigurationbuilder (); /// <summary> ///gets the contents of the configuration file, inherited from IConfiguration/// </summary> Private StaticIconfigurationroot _configuration; Static voidMain (string[] args) {_configuration=Configurationbuilder. Setbasepath (Directory.GetCurrentDirectory ()). Addjsonfile (CFG={cfg. Path="Config.json"; Cfg. Reloadonchange=true; Cfg. Optional=false; }) //The build method is called after Addjsonfile, otherwise the generated Iconfigurationroot instance's//The Providers property does not contain any elements and prevents the information in the file from being read . Build (); varurl = _configuration. GetSection ("URL"); //read JSON object, in IConfiguration, the key in the configuration file is flat varPAge = _configuration["Person:age"]; //traverse all elements in a configuration file foreach(varKeyValuePairinch_configuration. AsEnumerable ()) {Console.WriteLine ($"{Keyvaluepair.key}-{Keyvaluepair.value}"); } } }}
You can also map the contents of a file to an object, requiring additional references to the Microsoft.Extensions.Configuration.Binder
package:
classconfigmodel{ Public stringURL {Set;Get; } PublicPerson Person {Set;Get; }} Public classperson{ Public stringName {Set;Get; } Public intAge {Set;Get; }}varConfigmodel =NewConfigmodel (); Configurationbuilder.build (). Bind (Configmodel);
in ASP. NET core, you can also use ioptions<t> to get configuration information, but the content in,ioptions<t> does not change when the configuration file is modified.
Public Get ; } Public void configureservices (iservicecollection services) { services. Configure<ConfigModel>(Configuration);} // and get ioptions<t> through the IOC . Public void Method (ioptions<configmodel> opt) {}
Above, is the operation of the JSON file, the next operation of the XML file.
To read a read or, as in the. NET Framework App.config
Web.config
, introduce a System.Configuration.ConfigurationManager
package:
var value = configurationmanager.appsettings["key"]; Console.WriteLine (value); Console.WriteLine (configurationmanager.connectionstrings["connstr"). ConnectionString);
Or, reference the Microsoft.Extensions.Configuration.Xml
package,
_configuration = Configurationbuilder . Setbasepath (Directory.GetCurrentDirectory ()) . Addxmlfile ("app. config") . Build (); // It is better to use ConfigurationManager, adding multiple keys under the appsettings node can also cause a read-time throw exception var value = _configuration. GetSection ("appSettings:add:value"). Value;
Recommended Reading
Building a specification REST API with ASP. NET Core 2.0--preparatory Knowledge (2) + Preparing the project
Copyright notice
This article is the author original, the copyright belongs to the author Snow Feihong all. Reproduced must retain the integrity of the article, and in the page marked the location of the original link.
If you have questions, please email and contact the author.
. NET Core Read configuration files