[Selfless sharing: ASP. net core Project Practice (Chapter 8)] Read the configuration file (ii) read the custom configuration file, asp. netcore
Directory Index
[Selfless sharing: ASP. net core project practice] Directory Indexing
Introduction
We introduced how to read the appsettings. json file in the configuration file (1.
However, a problem arises: we are using Startup. in cs (for example), there are two problems: ① if we define N types of configuration, do we need to add N Such configurations here; ② If our configuration does not want to be written in appsettings. in json
Solve the problem
With the above two problems, we first add a configuration file siteconfig. json
{
"SiteBaseConfig ":{
// File Upload path
"FileUpPath": "/upload /",
// Whether to enable single-user logon
"IsSingleLogin": "True ",
// File format that can be uploaded
"AttachExtension": "gif, jpg, jpeg, png, bmp, rar, zip, doc, docx, xls, xlsx, ppt, pptx, txt, flv, apk, mp4, mpg, ts, mpeg, mp3, bak, pdf ",
// The maximum size of uploaded images is KB.
"AttachImagesize": 12400
}
}
The configuration class ApplicationConfiguration in reading file configuration (1)
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 to enable single user login
11 /// </ summary>
12 public bool IsSingleLogin {get; set;}
13 /// <summary>
14 /// Allowed file formats to be uploaded
15 /// </ summary>
16 public string AttachExtension {get; set;}
17 /// <summary>
18 /// Image upload maximum KB
19 /// </ summary>
20 public int AttachImagesize {get; set;}
21 #endregion
twenty two }
Add "copyToOutput": "siteconfig. json" to buildOptions of project. json so that the file will be automatically copied to the output directory (the bin directory by default) during compilation ):
Modify the File Reading configuration (1) domain layer appenders taionservices
Public class appenders taionservices
{
Public T getdeleetask< 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;
}
}
Note: we first created a ConfigurationBuilder object and registered a JsonConfigurationSource on it. When creating this JsonConfigurationSource object, in addition to specifying the path of the configuration file ("siteconfig. json"), we also set its ReloadOnChange attribute to True. The ReloadOnChange attribute indicates whether to re-load the configuration when the content of the original configuration file changes.
At this time, we will find Configure <T> (config. GetSection (key) error:
I checked services in the previous Startup. configure <T> () found that there are two parameters in this reload: IConfiguration and System. action <T>, why cannot I reload it here?
I was puzzled and finally found that the extension class Microsoft. Extensions. Options. ConfigurationExtensions was missing.
We have solved the Install-Package Microsoft. Extensions. Options. ConfigurationExtensions problem through NuGet. Thank you for @ Artech.
Let's test:
OK. In this way, the two questions we raised at the beginning of this article have been solved!
I hope to study Asp.net Core with you.
At the beginning, there was a limited level of contact, and many things were self-understanding and reading the information of the great gods on the Internet. If there is something wrong or incomprehensible, I hope you can correct it!
Although Asp.net Core is very popular now, many materials on the Internet are copied in the previous article, so I have not solved many problems for the time being. I hope you can help me together!
Original article reprinted please respect labor results http://yuangang.cnblogs.com