如何在运行时在 Blazor WebAssembly 客户端应用程序中使用 Azure 应用程序设置作为 appsettings.json 配置?

waf*_*ers 10 azure azure-web-app-service blazor blazor-client-side blazor-webassembly

在此输入图像描述

  • 我正在开发 Blazor WebAssembly 客户端/服务器项目(目录结构如上)
  • 在客户端和服务器项目中都有一些应用程序设置。
  • 这些项目托管在 Azure 中。

问题出在客户appsettings.json

  • 在客户端,appsettings.json位于目录内wwwroot。可以在应用程序内访问该文件,但是该设置不能被应用程序服务的 Azure Portal 应用程序设置覆盖。

这意味着,在将应用程序部署到 Web 应用服务上的 Azure 门户中后,我的配置设置不适用于应用程序设置的变量。

这是 中的代码Progam.cs,它工作正常并从文件中读取配置,但忽略 Azure 上的 Web 应用服务的配置设置。

public static async Task Main(string[] args)
{
    var builder = WebAssemblyHostBuilder.CreateDefault(args);
    builder.RootComponents.Add<App>("app");
    //Add a named httpClient and set base Address and Default Request Headers
    builder.Services.AddHttpClient("SOME_WEB_URL", client =>  // SOME_WEB_URL is defined in the appsettings.json or in the Azure App Service configuration (Application Settings)
    {
        client.BaseAddress = new Uri(builder.Configuration["sbformsapi"]);
    });

    //Add a named httpClient and set base Address and Default Request Headers
    builder.Services.AddHttpClient("WEB_APP_API", client =>   // WEB_APP_API is defined in the 
    {
        client.BaseAddress = new Uri(builder.Configuration["sbwebappapi"]);
    });

    builder.Services.AddAuthorizationCore();
    ....
    await builder.Build().RunAsync();
}
Run Code Online (Sandbox Code Playgroud)

有人可以指导我怎样才能

  • 将 appsettings.json 文件设置在 wwwroot 之外并从那里读取它?或者
  • 在运行时注入/使用 Azure 应用服务配置的应用程序设置中的值?

我在这里谈论应用程序设置(如图所示)...

在此输入图像描述

Mil*_*len 0

目前应用程序设置仅适用于与 Blazor 应用程序关联的后端 API(假设使用静态应用程序?)。

https://learn.microsoft.com/en-gb/azure/static-web-apps/application-settings