ASP. NET Core Configuration

Source: Internet
Author: User

The configuration system for ASP. NET core is different from previous versions of ASP. System.Configuration and XML configuration file Web. config, which now supports various formats, is much more flexible than before.
Support several:
Memoryconfigurationprovider
Environmentvariablesconfigurationprovider
Commandlineconfigurationprovider
Jsonconfigurationprovider
Xmlconfiguationprovider
Iniconfigurationprovider
Custom Configurationprovider
Today we look at the use of Jsonconfig file:

Confgurationsource: A data source equivalent to a accessory, which can be memory and can be a configuration file. Configurationbuilder: Accessory Data Source Assembly workshop, is responsible for removing the data source for configuration class consumption configurations. Configuration: A mapping configuration class definition for the data: Public classcompanyinfooptions{ Publiccompanyinfooptions (iconfiguration config) { This. name=config["name"]; This. address=config["Address"]; This. CreateDate = DateTime.Parse (config["CreateDate"]);} PublicString name{Get;Set;}  PublicString address{Get;Set;}  PublicDateTime createdate{Get;Set;}}
Vscode File Organization:
{"CompanyInfo": {"Name": "hbb0b0","address": "Xi ' an Jinyelu number6"," CreateDate ":" 2017/08/06 "}}

Note the configuration file is in the running directory, and the configuration files that are different from the previous configuration are not copied to the bin directory.

called in main:classprogram{//section nameConst stringstr_sectioncompanyinfo="CompanyInfo";Static voidMain (string[] args) {Console.WriteLine (Directory.GetCurrentDirectory ());//Configtionbuilder InitializationvarConfig =NewConfigurationbuilder (). Setbasepath (Directory.GetCurrentDirectory ()). Addjsonfile ("Companyinfo.json", Optional:true, Reloadonchange:true). Build ();//Initialize Companyinfooptionscompanyinfooptions Options=Newcompanyinfooptions (config. GetSection (Str_sectioncompanyinfo)); Output
Console.WriteLine ($"name:{options. Name}"); Console.WriteLine ($"address:{options. Address}"); Console.WriteLine ($"createdate:{options. Createdate.tostring ()}");}}

ASP. NET Core Configuration

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.