[Selfless sharing: ASP. net core Project Practice (Chapter 6)] Read the configuration file (1) appsettings. json, and machine learning practice Chapter 6
Directory Index
[Selfless sharing: ASP. net core project practice] Directory Indexing
Introduction
In our previous Asp.net mvc development, we couldn't help but think of web. config and app. config. In core, we have seen a lot of changes. The new configuration system is more lightweight, with better scalability and supports diversified data sources.
The blog Park has many explanations for this, such as Artche. However, it is still difficult to read the blog of old A without any basic knowledge. I am also confused about the configuration introduced by old, in the subsequent articles, I will explain it again in a way that is acceptable to cainiao like ours.
Today, let's take deleettings. json as an example to read some simple system configurations.
Appsettings. json
In Chapter 2, when we talk about the release of EF. cs adds services. addDbContext <ApplicationDbContext> (options => options. useSqlServer (Configuration. getConnectionString ("SqlServerConnection"); you have used appsettings. json
Let's add some simple system configurations to demonstrate how to read etettings. json:
{
"ApplicationInsights ":{
"InstrumentationKey ":""
},
"ConnectionStrings ":{
"SqlServerConnection": "Server =.; Database = db_wkmvc; User ID = sa_wkmvc; Password = 123456 ;"
},
"Logging ":{
"IncludeScopes": false,
"LogLevel ":{
"Default": "Debug ",
"System": "Information ",
"Microsoft": "Information"
}
},
"ApplicationConfiguration ":{
// 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
}
}
We add a configuration class 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 to enable single-user logon 11 /// </summary> 12 public bool IsSingleLogin {get; set ;} 13 /// <summary> 14 /// file format 15 /// </summary> 16 public string AttachExtension {get; set ;} 17 /// <summary> 18 /// maximum Image Upload value: KB19 /// </summary> 20 public int AttachImagesize {get; set;} 21 # endregion22}
Add ConfigureServices in Startup. cs
Services. Configure <ApplicationConfiguration> (Configuration. GetSection ("ApplicationConfiguration "));
Add a domain layer appenders taionservices
Public class appenders taionservices
{
Private readonly IOptions <ApplicationConfiguration> _ appConfiguration;
Public AppConfigurtaionServices (IOptions <ApplicationConfiguration> appConfiguration)
{
_ AppConfiguration = appConfiguration;
}
Public ApplicationConfiguration AppConfigurations
{
Get
{
Return _ appConfiguration. Value;
}
}
}
Add reference to using Microsoft. Extensions. Options;
Let's test:
Test results:
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