ASP. NET Core configuration file (ubiquitous dependency injection)

Source: Internet
Author: User

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)

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.