ASP. NET Core Read appsettings

Source: Internet
Author: User

Http://www.tuicool.com/articles/rQruMzV

Today, I've rewritten the previous ASP. NET MVC5 Demo project to ASP. NET Core to discover the original configurationmanager.appsettings[we have been using] The method of reading the appsettings node in Web. config is useless.. NET core has a lot of new practices and I've chosen the one that best fits my own project to share my implementation.

First, the original code:

Web. config

...  <    appsettings>    ... <key=value=<key=value=</appsettings>     

Controller:

Getblobcontainer() {    string connectionString = webconfigurationmanager.appsettings[" Storageconnectionstring "];. Blobclient.getcontainerreference (webconfigurationmanager.appsettings[return container;}   

This is also the way that we have been reading Web. config since ASP. If you want to know more about the way of forcing, you can refer to my article: "How to read the appsettings in Web. config", the article solves the problem is a strong type of configuration items, but ASP. NET core can be more convenient to implement this force lattice.

First, the setup file for ASP. NET core is Appsettings.json, not web. config, and for ASP. Web. config is just a configuration for IIS when deploying to Windows Server, and does not have a single-bit egg relationship with ASP.

The format for this Appsettings.json definition is as follows:

{    "Logging": {        false,        "LogLevel": {            "information"    }}}

I add my own configuration items to write this:

{    "Logging": {        false,        "LogLevel": {            "Yourcontainername"}}   

Next, create a new C # class that corresponds to your configuration entry:

appsettings{    set;}}

Then open the Startup.cs and change the Configureservices method to this (assuming you are a Web site created with ASP. NET Core's website application template)

Configureservices(iservicecollection services) {    //ADD framework services. Services. AddOptions (); Services. Configure<appsettings> (Configuration.getsection ("AppSettings")); services. Addmvc ();}  

This method is to do the IOC, is a mode of loading, we have to put in the force is snippet

Microsoft. Extensions. Options. Configurationextensions  

So, make sure you have this in your Project.json:

"Dependencies": {...    "1.0.0",...},  

The configuration object, which has been automatically applictaion in the default template of the ASP. NET Core Web, has the following code:

Startup (ihostingenvironment env) {    new Configurationbuilder (). Setbasepath (env. Contentrootpath). Addjsonfile (true). Addjsonfile ($ "appsettings.  true). Addenvironmentvariables (); Configuration = Builder. Build ();} get;}     

Then Configuration.getsection ("AppSettings") in this "AppSettings" corresponds to the JSON file is just configured in the "AppSettings" node.

Services. configure<appsettings> (Configuration. GetSection ("AppSettings"));    

This line of code means that once we use the AppSettings type in our application, we replace it with the result of Configuration.getsection ("AppSettings"),. NET The core will automatically help us with the type conversion and mapping, putting the force of my "how to read the appsettings in Web. config" in the "How High-Grid" load.

Finally, when you use the controller, you have to follow the IOC's usual method of loading the constructor into this way:

Set }homecontroller (ioptions<appsettings> settings) {AppSettings = settings. Value;} 

Then you can happily use the strongly-typed config:

Private Cloudblobcontainer Getblobcontainer () {    string connectionString = appsettings.storageconnectionstring;    Cloudstorageaccount storageaccount = Cloudstorageaccount.parse (connectionString);    cloudblobclient blobclient = Storageaccount.createcloudblobclient ();    cloudblobcontainer container =        blobclient.getcontainerreference (appsettings.azurestorageaccountcontainer );    return container;}    

ASP. NET Core Read appsettings

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.