How to add and retreive value from appsetting.json or some other configs in Razor Component though Razor Class Library?

mehmood tekfirst 766 Reputation points


I need to add some configs like appsetting.json in Razor Class Library?

And then I need to get the specific value from this config and need to use in Razor.Component .

A set of technologies in the .NET Framework for building web applications and XML web services.
2,814 questions
No comments
{count} votes

1 answer

Sort by: Newest
  1. Zhi Lv - MSFT 21,706 Reputation points Microsoft Vendor

    Hi @mehmood tekfirst ,

    The Razor Class Library is a class library, Razor views, pages, controllers, page models, Razor components, View components, and data models can be built into a Razor class library (RCL), but the RCL should not use application settings or the configs like the appsetting.json file.

    You can add the appsessting.json file in the Core web/MVC application, then provide those settings to the components via the Component parameters.

    For example, in the MVC application, add the appsetting.json file like this:


    Then, when calling the component from the RCL, register the IConfiguration in the razor view/page, and transfer the data to the RCL component via the Component parameters:


    the RCL component :


    And then, the result like this:


    You can view the source code from here: 208102-sourcecode.txt


    Here is another method to transfer data from parent to child component:


    @page "/parameter-parent"  
    <h1>Child component (without attribute values)</h1>  
    <ParameterChild />  
    <h1>Child component (with attribute values)</h1>  
    <ParameterChild Title="Set by Parent"  
                    Body="@(new PanelBody() { Text = "Set by parent.", Style = "italic" })" />  


    <div class="card w-25" style="margin-bottom:15px">  
        <div class="card-header font-weight-bold">@Title</div>  
        <div class="card-body" style="font-style:@Body.Style">  
    @code {  
        public string Title { get; set; } = "Set By Child";  
        public PanelBody Body { get; set; } =  
                Text = "Set by child.",  
                Style = "normal"  


    public class PanelBody  
        public string? Text { get; set; }  
        public string? Style { get; set; }  

    Refer this article: Component parameters

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,