"selfless sharing: ASP (eighth)" Read the configuration file (ii) read the custom configuration file

Source: Internet
Author: User

"selfless sharing: asp. Net CORE Project combat" Catalog Index

Brief introduction

We described in reading the configuration file (a) appsettings.json, How to read the Appsettings.json.

But then there is the problem: we are using Startup.cs (such As) to implement the configuration read, there are two problems ① if we define n configuration, whether we want to add here N such a configuration; ② if our configuration doesn't want to write in appsettings.json?

  

  

Solve the problem

With the two questions above, let's start by adding a configuration file Siteconfig.json

  

{
"sitebaseconfig": {
File Upload Path
"fileuppath": "/upload/",
Whether to enable Single-user sign-on
"issinglelogin": "True",
Allow upload of file formats
"attachextension": "gif,jpg,jpeg,png,bmp,rar,zip,doc,docx,xls,xlsx,ppt,pptx,txt,flv,apk,mp4,mpg,ts,mpeg,mp3,bak , PDF ",
Picture Upload max kb
"attachimagesize": 12400
}
}

We are reading the configuration class in the file configuration (a) applicationconfiguration

1 public class ApplicationConfiguration 2     {3         #region attribute member 4  5//         <summary> 6//         File upload path 7
   
    ///</summary> 8 Public         string Fileuppath {get; set;} 9         //<summary>10//whether single-user login is enabled         // /</SUMMARY>12 public         bool Issinglelogin {get; set;} ///<summary>14///         </summary>16 public         string Attachextension { Get Set }17///<summary>18///         image upload maximum KB19//         </summary>20 public         int Attachimagesize {get ; Set }21         #endregion22     }
   

In Project.json buildoptions add "copytooutput": "siteconfig.json", so that the file is compiled automatically copied to the output directory (the default is the bin directory):

  

Modify the domain layer of the Read file configuration (i) appconfigurtaionservices

public class Appconfigurtaionservices
{
    

Public T getappsettings<t> (string Key) where T:class,new ()
{
iconfiguration config = new Configurationbuilder ()
. Add (new jsonconfigurationsource {path= "siteconfig.json", Reloadonchange=true})
. Build ();

var appconfig= new Servicecollection ()
. AddOptions ()
. Configure<t> (config. GetSection (key))
. Buildserviceprovider ()
. Getservice<ioptions<t>> ()
. Value;

Return appconfig;
}

}

  

Description: we first created a Configurationbuilder object and registered a jsonconfigurationsource on it. When creating this Jsonconfigurationsource object, in addition to specifying the path to the configuration file ("siteconfig.json"), We also set its Reloadonchange property to True. The meaning of this Reloadonchange property is whether the configuration needs to be reloaded when the content of the original configuration file Changes.

At this point we will find configure<t> (config. GetSection (key)) error:

I looked at the services we had before in Startup. Configure<t> () found that the parameter inside this is a two-overloaded one is the iconfiguration one is system.action<t>

Very puzzled, and finally found that there is less extension class Microsoft.Extensions.Options.ConfigurationExtensions

We've solved this problem by adding this install-package Microsoft.Extensions.Options.ConfigurationExtensions through NuGet, thanks @Artech

Let's test it out:

  

  

OK, so the two questions we put forward at the beginning of the article are solved!

"selfless sharing: ASP (eighth)" Read the configuration file (ii) read the custom configuration file

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.