NET Core Development-read config file configuration

Source: Internet
Author: User
Tags connectionstrings

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/

NET Core Development-read config file 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.