How can I add service and get setting from appsetting.json in razor class library(RCL)?

melon NG 291 Reputation points

I need to add Google reCAPTCHA to my site and I achieve this by Unicorn.reCAPTCHA.AspNetCore(

There are some sub-domain sites also. I coded an RCL to share their header/footer and some codes of reCAPTCHA.

Before the reCAPTCHA runs, it should add the service in startup.cs like this:

And the Section in appsetting.json is just like this:
"RecaptchaSettings": {
"SiteKey": "aaaaa",
"SecretKey": "bbbb",
"Version": "v3",
"Domain": ""

Now I have to add the codes above for setting the reCAPTCHA in every appsetting.json and starup.cs of the sites, and all the codes are the same, it is so troublesome.

Is there any way for me to get the setting in appsetting.json only once and add the service in RCL? Thank you.

A set of technologies in the .NET Framework for building web applications and XML web services.
4,234 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce ( 57,731 Reputation points

    normally you would supply a IServiceCollection extension helper that added the configuration to the Services for DI.:


    simple implementation

     public static IServiceCollection AddReCaptcha(this IServiceCollection services, IConfiguration config, string sectionName = "reCaptchaOptions")  
         if (!services.Any(x => x.ServiceType == typeof(ReCaptchaOptions)))  
              var ReCaptchaOptions = new ReCaptchaOptions();  
         return services;  

  2. AgaveJoe 26,161 Reputation points

    Again, RCL is just a class library and configuration can be injected into any constructor including a class library. The pattern in the link you provided is used in a service pattern. You have not provided any code that shows your service which makes providing an accurate solution difficult at best.

    however, I created an example of fetching configuration in a RCL Razor page using the official RCL and Option pattern documentation.

    Create reusable UI using the Razor class library project in ASP.NET Core
    Bind hierarchical configuration data using the options pattern

    RCL Solution Explorer


    Option model

        public class RecaptchaSettingsOptions  
            public const string RecaptchaSettings = "RecaptchaSettings";  
            public string SiteKey { get; set; } = string.Empty;  
            public string SecretKey { get; set; } = string.Empty;  
            public string Version { get; set; } = string.Empty;  
            public string Domain { get; set; } = string.Empty;  


    @model RazorClassLib.MyFeature.Pages.Page1Model  
    <!DOCTYPE html>  
        <meta name="viewport" content="width=device-width" />  
        <div>Hello World</div>  
        <div>SiteKey = @Model.Options.SiteKey</div>  
        <div>SecretKey = @Model.Options.SecretKey</div>  
        <div>Version = @Model.Options.Version</div>  
        <div>SiteKey = @Model.Options.Domain</div>  


    using Microsoft.AspNetCore.Mvc;  
    using Microsoft.AspNetCore.Mvc.RazorPages;  
    using Microsoft.Extensions.Configuration;  
    using Microsoft.Extensions.Options;  
    using RazorClassLib.Options;  
    namespace RazorClassLib.MyFeature.Pages;  
    public class Page1Model : PageModel  
        private readonly IConfiguration _configuration;  
        public Page1Model(IConfiguration configuration)  
            _configuration = configuration;  
        public void OnGet()  
            Options = new RecaptchaSettingsOptions();  
        public RecaptchaSettingsOptions Options { get; set; }  

    Results of https://localhost:7143/myfeature/page1

    Hello World  
    SiteKey = aaaaa  
    SecretKey = bbbb  
    Version = v3  
    SiteKey =