c# - Blazor Webassembly 配置 (appsettings.json) 最初在组件中加载空值
问题描述
Blazor WebAssembly 应用程序在执行导致 Null 初始化的方法后加载配置值,然后它将返回正确的值。
我遵循了 Microsoft 文档。
应用程序文件:
wwwroot/appsettings.json
我的 Component.razor:
@page "/"
<h1>Configuration example</h1>
<p>Message: @Configuration["message"]</p>
@code {
protected override async Task OnParametersSetAsync()
{
await trying to read Configuration["Message"];
}
}
我的 Program.cs 文件:
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
await builder.Build().RunAsync();
}
}
我的 _Imports.razor
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.WebAssembly.Http
@using Microsoft.Extensions.Configuration
@using Microsoft.JSInterop
@inject IConfiguration Configuration;
注意:了解我得到的值是正确的,但总是首先为空,然后使用正确的值重新渲染,我使用 OnParametersSetAsync() 作为入口点,我理解 OnParametersSetAsync() 每次参数更改都会发生更改。但是配置值首先应该是常量而不是 null 对吗?
解决方案
推荐阅读
- node.js - 将数组改回json格式后如何返回?
- prolog - DCG:零次或多次,零次或一次,一次或多次出现?
- google-oauth - 使用 Google Oauth 登录 Jupyterhub 时出错
- spring-boot - Spring Boot 中多对一 rs 请求中一篇帖子的 Json 内容
- python - 无法使用“python -i”第二次打开我的源代码
- python - 如何让 IPython 在 python 版本 3.9.1 而不是旧版本 3.7 上运行?
- interactive-brokers - 盈透证券是这个错误吗?10197“竞争实时会话期间没有市场数据”
- c++ - 在智能指针向量的动态创建元素上强制转换 static_cast
- android - 如何在firebase recyclerview android中实现分页或无限滚动
- python - Go 中是否有相当于 Python 的“目录”?