Read the configuration file and asp.net configuration file in ASP. NET 5.
(This article is also published in my public account "dotNET daily excellent article". Welcome to the QR code on the right to follow it .)
In ASP. NET 5, the basic structure of the previous configuration file is abandoned, and a new configuration file system is introduced. The article recommended today briefly introduces this aspect.
Microsoft introduced a completely new configuration file infrastructure in the cross-platform runtime to be officially released. Based on this configuration system, it is never so easy to read content from multiple configuration files.
The configuration system not only supports configuration files in json, xml, and ini formats, but also supports reading content from environment variables. As follows:
var configuration = new Configuration() .AddJsonFile("config.json") .AddIniFile("config.ini") .AddXmlFile("config.xml") .AddEnvironmentVariables();
To support json files, Microsoft. framework. configurationModel. json namespace. xml files are supported and Microsoft. framework. configurationModel. xml, while the INI file requires the introduction of Microsoft. framework. configurationModel.
For json configuration files like the following:
{ "Data": { "DefaultConnection": { "ConnectionString": "Server=(localdb)\\mssqllocaldb;Database=DbName;Trusted_Connection=True;MultipleActiveResultSets=true" } }}
Reading the content is very simple:
var connectionString = configuration.Get("Data:DefaultConnection:ConnectionString");
"Read the original text" you can browse the original text (the original content is also relatively simple, almost like this ).
Address: http://blog.developers.ba/read-config-file-in-asp-net-vnext/