Search code examples
firebaseazureazure-web-app-servicefirebase-adminazure-app-configuration

How do I set json file(json file's path) to a value in Azure App settings?


How do I set json file(json file's path) to a value in Azure App settings?
I am adding Firebase SDK to the server.
I defined "/my-json-file.json" as GOOGLE_APPLICATION_CREDENTIALS key's value.

enter image description here


Because "/" means "\site\wwwroot" (root directory).
But it didn't worked. I define encironment variable in my desktop,
so i checked completely in local environment.
I have to make it work at the production, test, etc environment too.
Is there any information about this?

Solution

    • To set JSON content type for key-values, we need to add the JSON values in Azure App Configuration => Configuration Explorer Section.

    enter image description here

    Check the below workaround for creating and consuming JSON key-values

    In Azure Portal => App Configurations => Create

    • Make sure you have a valid JSON content type. I have added application/json content type.

    We can create JSON key-values from Azure portal, Azure CLI or even you can create a json file and import values.

    Here I have created from portal => App Configuration => Configuration Explorer

    enter image description here

    Consume JSON values in WebApp

    Create ASP. Net Core App, navigate to the project root directory and run the below command to add the UserSecretID

    dotnet user-secrets init
    

    Install the Microsoft.Azure.AppConfiguration.AspNetCore NuGet Package

    My Program.cs

    var builder = WebApplication.CreateBuilder(args);
    var connectionString = builder.Configuration.GetConnectionString("AppConfig");
    builder.Host.ConfigureAppConfiguration(builder =>
    {    
        builder.AddAzureAppConfiguration(connectionString);
    })
                .ConfigureServices(services =>
                {
                    services.AddControllersWithViews();
                });
    
    var app = builder.Build();
    
    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    app.UseRouting();
    app.UseAuthorization();
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    app.Run();
    
    • Now we need to display the values which we have stored in Azure App Configuration

    My Index.html of Home Controller

    @{
        ViewData["Title"] = "Home Page";
    }
    @using Microsoft.Extensions.Configuration
    @inject IConfiguration Configuration
    <div class="text-center">
        <h1 class="display-4">Welcome</h1>
        <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    </div>
    <style>
        body {
            background-color: @Configuration["Settings:BackgroundColor"]
        }
    
        h1 {
            color: @Configuration["Settings:FontColor"];
            font-size: @Configuration["Settings:FontSize"]px;
        }
    </style>
    
    <h1>@Configuration["Settings:Message"]</h1>
    

    I have set Background color as yellow in JSON values, you can see it is applied.

    Output:

    enter image description here

    Refer the official MSDoc for binding hierarchical configuration data.