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

mehmood tekfirst 771 Reputation points
2022-06-02T10:54:57.337+00:00

Hi,

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 .

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,192 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2022-06-03T05:17:24.097+00:00

    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 Asp.net 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:

    208010-image.png

    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:

    208086-image.png

    the RCL component :

    208064-image.png

    And then, the result like this:

    208075-image.png

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

    Update:

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

    Pages/ParameterParent.razor:

    @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" })" />  
    

    Shared/ParameterChild.razor:

    <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">  
            @Body.Text  
        </div>  
    </div>  
      
    @code {  
        [Parameter]  
        public string Title { get; set; } = "Set By Child";  
      
        [Parameter]  
        public PanelBody Body { get; set; } =  
            new()  
            {  
                Text = "Set by child.",  
                Style = "normal"  
            };  
    }  
    

    PanelBody.cs:

    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,
    Dillion

    0 comments No comments