我正在 Azure 静态 Web 应用程序上运行 Blazor WASM 应用程序。 在我的 .NET6 C# 应用程序中,我想读取我在门户中添加的设置。 我没有使用也不想使用 appsettings.json 或 web.config。我想读取我在门户中添加的值。
我已经读了好几天了。我找到了很多有关服务器端解决方案的页面,但我无法使用这些页面,因为我只运行客户端。
我认为我需要在我的program.cs中做一些事情:
static void ConfigureServices(IServiceCollection services, string baseAddress)
{
services.AddTransient(_ => new HttpClient(new DefaultBrowserOptionsMessageHandler(new HttpClientHandler())
{
DefaultBrowserRequestCache = BrowserRequestCache.ForceCache,
DefaultBrowserRequestMode = BrowserRequestMode.Cors
})
{
BaseAddress = new Uri(baseAddress)
});
// TODO make the settings from the portal available for all pages and components
}
我不确定如何注入(inject)设置。
我想我需要在我的页面上使用它,如下所示:
@page "/"
@inject IConfiguration _configuration
<h1>Hello, world!</h1>
<p>Welcome to your new app.</p>
<p>Config: @_configuration["Foo"]</p>
<p>Env: @Environment.GetEnvironmentVariable("Foo")</p>
<SurveyPrompt Title="How is Blazor working for you?" />
请指教。
最佳答案
目前,应用程序设置仅适用于与 Blazor 应用关联的后端 API(使用 Azure 静态 Web 应用时)。
请引用Configure application settings for Azure Static Web Apps获取详细信息。
关于azure - 使用 Blazor WASM 读取静态 Web 应用服务的 Azure 门户应用程序设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70461295/