【发布时间】:2022-01-24 09:36:52
【问题描述】:
我正在 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
}
我不确定如何注入设置。
我想我需要像这样在我的页面上使用它:
@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?" />
请指教。
【问题讨论】:
-
您找到解决方案了吗?
-
我没有找到解决方案。我正在使用一种解决方法,即根据我是处于开发模式(本地)还是生产模式(已发布)对我需要的变量进行硬编码。
标签: azure configuration blazor-webassembly azure-static-web-app