"Selfless Sharing: ASP (seventh)" Read the configuration file (ii) read the custom configuration file

Source: Internet
Author: User

Catalog Index 

"Selfless sharing: ASP. NET CORE Project Combat" Catalog Index

Introduction

we are Read configuration file (i) Appsettings.json describes 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 ; ② What if our configuration doesn't want to be written 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 single-user logon is enabled
"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 classapplicationconfiguration2     {3         #regionAttribute members4 5         /// <summary>6         ///File Upload path7         /// </summary>8          Public stringFileuppath {Get;Set; }9         /// <summary>Ten         ///whether to enable single-user sign-on One         /// </summary> A          Public BOOLIssinglelogin {Get;Set; } -         /// <summary> -         ///allow upload of file formats the         /// </summary> -          Public stringattachextension {Get;Set; } -         /// <summary> -         ///picture upload max KB +         /// </summary> -          Public intattachimagesize {Get;Set; } +         #endregion A}

  In Project.json buildoptions add "copytooutput": "Siteconfig.json", Allow the file to be automatically copied to the output directory at compile time (the bin directory is the default):

  

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 Jsonconfigurationsourceon it. When creating this Jsonconfigurationsource object, in addition to specifying the path to the configuration file ("Siteconfig.json"), we also The Reloadonchange property is set 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>, So why isn't there a heavy load here?

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

  We have added this install-package Microsoft.Extensions.Options.ConfigurationExtensions issue through NuGet to be resolved, thanks in this @Artech

  Let's test it out:

  

  

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

I want to learn from you. ASP.

Just beginning to contact, the level is limited, a lot of things are their own understanding and flipping through the online god of information, if there are wrong places and do not understand the place, I hope you correct!

Although ASP. NET Core is now very hot, but many of the online information is the copy of the previous article, so there are a lot of problems I have not resolved, I hope we can help together!

Original article reproduced please respect the fruits of labor http://yuangang.cnblogs.com

"Selfless Sharing: ASP (seventh)" 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.