How to create and access custom application settings in your Microsoft ASP.NET Core application
By: Steve Adams,
As part of your development process, you will undoubtably need to add some custom application settings that will be used by your application. It's quite normal to add/set items like connection strings, application logging settings and more.
As you develop your application, you will likely need to have some application settings that rather than hard code in your application, you'd like to have them in a configuration. These settings values most often vary once you deploy your application to different environments (Development, Staging, Production).
When you create your new ASP.NET Core application, it creates an initial appsettings.json file you.
If you were not aware of this file, this is the new app.config / web.config file for ASP.NET Core applications.
The default AppSettings.json file is pretty bare.
1{2 "Logging": {3 "LogLevel": {4 "Default": "Information",5 "Microsoft.AspNetCore": "Warning"6 }7 },8 "AllowedHosts": "*"9}
Let's say we wanted to add some additional settings in there to store our settings for connecting to Google Recaptcha. We wanted to set values for SiteKey and SecretKey.
We can simply create a new section in our file, give it a name that describes the sections purpose and add the desired settings with their values.
The file would now look like this:
1{2 "Logging": {3 "LogLevel": {4 "Default": "Information",5 "Microsoft.AspNetCore": "Warning"6 }7 },8 "AllowedHosts": "*",9 "GoogleReCaptcha" : {10 "SiteKey": "your sitekey value",11 "SecretKey" : "your secret key value"12 }13}
So, we now have our new custom section in the appsettings.json file. Now how do we make the values accessible to our application?
First, let's create a new class in our application that will be used to hold these settings:
1namespace Sample.Models;23public class GoogleReCaptchaSettings4{5 public string SiteKey { get; set; }6 public string SecretKey { get; set; }7}
Now we can some new code in our application to allow these settings to be loaded and available via dependency injection (DI). If you are not familiar with DI, then you should make yourself familiar with it by referring to Microsoft's documentation, Dependency injection in ASP.NET Core
Depending on which version of the ASP.NET Core project template you have used to create your application, you will have either a Startup.cs and Program.cs classes in your project or only a Program.cs, but the principal of what you need to do is the same.
If you have a Startup.cs file, open that file and in the ConfigureServices method, add the following code:
1services.Configure<GoogleReCaptchaSettings>(_config.GetSection("GoogleReCaptcha"));
If you don't have a Startup.cs file, then open Program.cs and add the same line of code right before the builder.Build() call:
1services.Configure<GoogleReCaptchaSettings>(_config.GetSection("GoogleReCaptcha"));23var app = builder.Build();
This will now add an instance of IOptions<GoogleReCaptchaSettings> class to the DI container and you can now retrieve that on your classes whenever you need them.
Simply add an IOptions<GoogleReCaptchaSettings> parameter to the constructor of your class and ASP.NET Core will automatically pass the instance to your class. You can then use that instance to get your setting values.
1namespace Stecosoft.Core.Services;2public class GoogleReCaptchaService : IGoogleReCaptchaService3{4 private readonly GoogleReCaptchaSettings _googleReCaptchaSettings;56 public GoogleReCaptchaService(IOptions<GoogleReCaptchaSettings> googleReCaptchaSettings)7 {8 _googleReCaptchaSettings = googleReCaptchaSettings.Value;9 }1011 public async Task<bool> VerifyToken(string token)12 {13 .14 .15 .16 var url = $"https://www.google.com/recaptcha/api/siteverify?secret={_googleReCaptchaSettings.SecretKey}&response={token}&remoteip={remoteIp}";17 .18 .19 .20 }21}
Note: As we are receiving an instance of IOptions<GoogleReCaptchaSettings> in our constructor rather than simply an GoogleReCaptchaSettings instance, I have assigned googleReCaptchaSettings.Value to _googleReCaptchaSettings.
I hope that help to give you a simple explanation on how you can add and read custom settings from the appsettings.json file.