How to configure application settings in Appsettings.json, Microsoft gives the method: Https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration
Here is my approach:
Because I built an empty project, nothing, lots of things. New and referenced, create a new Appsettings.json file, and then add a appsettings field that contains the configuration and values
Create a AppSettingsModel.cs under the Models folder
The NuGet Package Manager references or writes the following packages in Project.json
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0", "Microsoft.Extensions.Configuration.Json": "1.0.0"
Then configure the Startup.cs in the
Public classStartup { PublicStartup (ihostingenvironment env) {varBuilder =NewConfigurationbuilder (). Setbasepath (env. Contentrootpath). Addjsonfile ("Appsettings.json", Optional:true, Reloadonchange:true); Configuration=Builder. Build (); } PublicIconfigurationroot Configuration {Get; } Public voidconfigureservices (iservicecollection services) {services. Addmvc ();Services. AddOptions (); Services. Configure<AppSettingsModel> (Configuration.getsection ("AppSettings")); } Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {The code here has nothing to do with the subject } }
Then modify the controller.
Public classBasiccontroller:controller {/// <summary> ///Get Accesstoken/// </summary> /// <returns></returns> Public stringGetaccesstoken (ioptions<appsettingsmodel> settings) { stringAccesstoken =Accesstokencontainer.trygetaccesstoken (settings. Value.weixinappid, settings. Value.weixinappsecret); returnAccesstoken; } }
This is the configuration is successful, you can also see this blogger's article: http://blchen.com/asp-net-read-config-from-appsettings-json/
ASP. NET core reads the configuration of Appsettings.json