. NET Core Read Appsettings.json

Source: Internet
Author: User

{  "Logging": {    "Includescopes":false,    "LogLevel": {      "Default":"Warning"    }  },  "AppSettings": {    "sqlserverconnection":"Server=127.0.0.1;database=demo; User Id=sa; Password=sa;",    "rediscaching": {      "Enabled":true,      "ConnectionString":"127.0.0.1:6379"    }  }}

1. Initial injection mode

Startup.cs under Configureservices method:

Services. AddOptions (); services. Configure<AppSettings> (configuration.getsection ("AppSettings"));
 Public classappsettings{ PublicRediscaching rediscaching {Get;Set; }} Public classrediscaching{/// <summary>     ///is enabled/// </summary>      Public BOOLEnabled {Get;Set; } /// <summary>     ///Link Information/// </summary>      Public stringConnectionString {Get;Set; }}

Then call in the controller:

 Public ReadOnly Ioptions<appsettings> Setting;  Public HomeController (ioptions<appsettings> setting) {    = setting;}  Public Iactionresult Index () {    var s = Setting.Value.RedisCaching.ConnectionString;    viewdata["ABC"] = s;     return View ();}

2. Read Appsettings.json mode (this method is required to reference Microsoft.Extensions.Options.ConfigurationExtensions in a separate class library)

1) First set the Appsettings.json file to the root directory. Right-Appsettings.json advanced copy to the output directory, select Always copy.

    

2)

 Public T get<t> (string  key)        {            new  Configurationbuilder (). Setbasepath (Directory.GetCurrentDirectory ())                . Addjsonfile ("appsettings.json"truetrue). Build ();            return config. GetSection (Key). Get<t>();        }
 Public iactionresult Index ()        {            var s = new getappsetting (). Get<string> ("Appsettings:sqlserverconnection");              viewdata["ABC"] = s;             return View ();        }

The first method in other class library can also be called, more convenient, just do the core project, with the first method when the class library is not good call, checked for half a day with the second kind of can.

. NET Core Read Appsettings.json

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.