How ASP. NET Core reads the configuration file, and today we're going to learn.
The configuration system for ASP. NET core is different from previous versions of ASP. System.Configuration and XML configuration file Web. config.
The new configuration system supports configuration files in a variety of formats.
Let's start with a JSON-formatted configuration file.
We create a new ASP. NET Core Web application and choose No authentication.
Reading configuration Files
In the project directory there is a Appsettings.json, we first to operate this file.
Add the following two nodes to the Appsettings.json.
{ "Data": "Linezero", "ConnectionStrings": { "defaultconnection": "Database 1", "devconnection": "Database 2" }, "Logging": { "includescopes": false, "LogLevel": { "Default": "Debug", "System": " Information ", " Microsoft ":" Information "}} }
Let's read it below. Since the project has already added the file to Configurationbuilder by default, we can read it directly.
Read in the Configure method:
public void Configure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) { var data = configuration["Data"]; Two ways of reading var Defaultcon = configuration.getconnectionstring ("defaultconnection"); var DevCon = configuration["Connectionstrings:devconnection"];
Debug program, you can see the data successfully removed.
Multi-environment differentiation
We copy a Appsettings.json and rename it to appsettings. Development.json
Change the appsettings. Development.json as follows:
{ "Data": "Linezero Development", "ConnectionStrings": { "defaultconnection": "Development Database 1", " Devconnection ":" Development Database 2 " }, " Logging ": { " includescopes ": false, " LogLevel ": { " Default ":" Debug ", " System ":" Information ", " Microsoft ":" Information "}} }
Then we debug the program and you will find that the obtained value becomes the value in the Development.json.
This is the multi-environment configuration.
Public Startup (ihostingenvironment env) { var builder = new Configurationbuilder () . Setbasepath (env. Contentrootpath) . Addjsonfile ("Appsettings.json", Optional:true, Reloadonchange:true) . Addjsonfile ($ "appsettings.{ Env. Environmentname}.json ", optional:true)//Add Environment Profile, new project by default . Addenvironmentvariables (); Configuration = Builder. Build (); }
If we directly execute the read is the value of Appsettings.json, because the direct execution is the Production environment.
Here is the output diagram:
When debugging:
Dotnet Run:
Object Read
We add a SiteConfig node to both Appsettings.json and Development.json.
"SiteConfig": { "Name": "Linezero ' s Blog", "Info": "ASP. NET Core development and cross-platform, profile read" },
Then create a new SiteConfig class.
public class SiteConfig {public string Name {get; set;} public string Info {get; set;} }
First, add options and corresponding configurations to the configureservices.
public void Configureservices (iservicecollection services) { //ADD framework services. Services. Addmvc (); Add Options Services. AddOptions (); Services. Configure<siteconfig> (Configuration.getsection ("SiteConfig")); }
Then we read it in the Controller.
public class Homecontroller:controller {public siteconfig Config; Public homecontroller (ioptions<siteconfig> option) { Config = option. Value; } Public Iactionresult Index () { return View (Config); } }
Corresponding View index.cshtml
@model siteconfig@{ viewdata["Title"] = model.name;}
Executing the program Http://localhost:5000/
If you think this article is helpful to you, please click " recommend ", thank you.
ASP. NET core development-read config file configuration