(This article also published in my public number "dotnet daily Essence article", Welcome to the right QR code to pay attention to. )
The infrastructure of the previous configuration file was discarded in ASP. NET 5, and a new configuration file system was introduced. The article recommended today is a brief introduction to this aspect.
Microsoft has introduced a new profile infrastructure in the upcoming cross-platform runtime, based on which it has never been so easy to read content from multiple configuration files.
This configuration system not only has a built-in configuration file that supports Json,xml and INI formats, it also supports reading content from environment variables. As follows:
var New Configuration () . Addjsonfile ("config.json") . Addinifile ("config.ini") . Addxmlfile ("config+ +") . Addenvironmentvariables ();
To support JSON files, Microsoft.Framework.ConfigurationModel.Json namespaces must be introduced, XML files must be introduced, MICROSOFT.FRAMEWORK.CONFIGURATIONMODEL.XML should be imported, and INI files should be introduced MICR Osoft. Framework.configurationmodel.
For a JSON configuration file like this:
{ "Data": {" defaultconnection": { "ConnectionString": "server= ( LOCALDB) \\mssqllocaldb;database=dbname; Trusted_connection=true; Multipleactiveresultsets=true" }} }
Reading the content is simple:
var connectionString = Configuration. Get ("Data:DefaultConnection:ConnectionString");
"Read the original" can browse the original text (the original content is relatively simple, almost like this).
Original address: http://blog.developers.ba/read-config-file-in-asp-net-vnext/
Reading a configuration file in ASP. NET 5