"Selfless sharing: ASP. NET CORE Project Combat (sixth)" Read the configuration file (i) Appsettings.json

Source: Internet
Author: User

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

Brief introduction

In our previous ASP. NET MVC development, when it comes to configuration files, we don't think of web. config and App. Config, in the core we see a lot of changes, the new configuration system appears to be more lightweight, has better extensibility, and supports a variety of data sources.

Blog Park for this explanation a lot, such as: Artche, but, there is no point to see the old a blog or some difficult, for the old a introduction of the configuration, I also look at the confused, in the following article, I will use like our rookie easy to accept the way, re-explain.

Today, we take Appsettings.json as an example to read some simple system configurations.

Appsettings.json

In Chapter Two, we add services in Startup.cs when we talk about EF online. adddbcontext<applicationdbcontext> (options = options. Usesqlserver (configuration.getconnectionstring ("sqlserverconnection")); Already used to Appsettings.json.

Let's add some simple system configuration to demonstrate the read Appsettings.json:

  

{
"Applicationinsights": {
"Instrumentationkey": ""
},
"ConnectionStrings": {
"Sqlserverconnection": "Server=.;D ATABASE=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 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 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 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     }
   

Added in Startup.cs's configureservices

Services. Configure<applicationconfiguration> (Configuration.getsection ("applicationconfiguration"));

  

Add a domain layer appconfigurtaionservices

public class Appconfigurtaionservices
{
Private ReadOnly ioptions<applicationconfiguration> _appconfiguration;
Public appconfigurtaionservices (ioptions<applicationconfiguration> appconfiguration)
{
_appconfiguration = appconfiguration;
}

Public ApplicationConfiguration Appconfigurations
{
Get
{
return _appconfiguration.value;
}
}
}

Add reference using Microsoft.Extensions.Options;

  

Let's test it out:

  

Test results:

  

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!

"Selfless sharing: ASP. NET CORE Project Combat (sixth)" Read the configuration file (i) Appsettings.json

Related Article

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.