Reading a configuration file in ASP. NET 5

Source: Internet
Author: User

(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

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.