Front Smoke:
The previous XML node configuration file was canceled in. NET Core, and the *.json format was used instead.
In the Startup.cs file, construct the method build Appsetting.json file,
This paper summarizes the official tool class of parsing configuration file;
I.. appsettings.json file
In the new Core Web project, there is a global configuration variable by default: Iconfigurationroot
The indexer and GetSection method are provided;
{ "Host":"http://localhost:5966/", "Title":"Hello World", "Sitekeywords":"NET , C #, ASP. NET core, ASP. NET EntityFramework Core", "Description":"DotNetClub:asp.net Core", "ConnectionString":"Data source=localhost;initial Catalog=dbname; User Id=username; Password=pwd;", "Redis": { "Endpoints": ["localhost","6379" ], "Password":"", "Db":"" }, "Site": { "Host":"http://localhost:5966", "Title":"Hello World", "Description":"DotNetClub:asp.net Core", "Allowregister":true, "Verifyregisteruser":false, "adminuserlist": [] }, "CookieName":"Dotnetclub.user", "Categories": [ { "Key":"Share", "Name":"Share" }, { "Key":"Ask", "Name":"FAQ" }, { "Key":"Job", "Name":"Recruitment" } ]}
To get the root node data:
configuration["ConnectionString"]
configuration["Title"]
Get node Data:
Services. Configure<redisoptions> (Configuration.getsection ("Redis"). Bind)
The focus is on the ioptions provided by this Microsoft.Extensions.Options.ConfigurationExtensions.
The Configure method binds a configuration in Appsettings.json to one of the entity objects. Such as:
Public classredisoptions {/// <summary> ///Redis end points, such as "{Host or Ip}:{port}"/// </summary> Public string[] Endpoints {Get;Set; } /// <summary> ///Redis Password/// </summary> Public stringPassword {Get;Set; } /// <summary> ///Default Redis Database/// </summary> Public intDb {Get;Set; } }
View Code
The completion of such a process is the Configure class implementation of a dependency injection;
ASP. NET Core configuration file (ubiquitous dependency injection)