Tail tooth coat


Five reading modes

  • Five reading methods depend on  IConfiguration  and  IConfigurationRoot  object

1、 Primary writing

//Case insensitive   string connectionString = _ configuration["ConnectionString"];

2、 Binding configuration model

var config = new Config01();

3、 Partial binding

//Websetting is a sub object of config. Var websetting = new websetting();

4、 Register services for configuration options

After registration, it can be used in other places through dependency injection. Through a  OptionsObject get configuration
You can get the configuration options through opt.value.xxx

5、 Read the user-defined configuration file through iconfigurationroot (appsettings.json is the system default)

First obtain the iconfigurationroot object, and the remaining operations are the same as the first four.

Unsolved mystery

If you read configuration files of different environments??????

That is right  appsettings.Development.json 、 appsettings.Production.json  Read of
It is known that by default, the priority of development and production is greater than appsettings.json. According to this feature, the public configuration can be stored in Appsettings. JSON
  • The development environment can be changed by modifying environment variables
  • Production environment by modifying web. Config

Or modify the folderprofile.pubxml file before publishing



Read multiple configuration files in the way of reading custom configuration files???

As far as we know, the latecomers are the best


It can be obtained by constructing injection

Tail tooth coatTail tooth coat

private IConfiguration _configuration;

public Startup(IConfiguration configuration)
    _configuration = configuration;

Structural injection


Iconfigurationroot is a sub interface of iconfiguration. It is generally used to read custom configuration files and the default configuration files of aspnet core, but it is a bit superfluous.

An instance of iconfigurationroot can be obtained through configurationbuilder

IConfigurationRoot root = new ConfigurationBuilder().AddJsonFile("").Build();
Usage is basically consistent with iconfiguration object