简体   繁体   English

Blazor Webassembly 配置 (appsettings.json) 最初在组件中加载 null 值

[英]Blazor Webassembly Configuration (appsettings.json) Loading null values in Component Initially

Blazor WebAssembly App Loads Configuration Values After Executing Methods Resulting in Null's Initialy then it will return with correct values. Blazor WebAssembly 应用程序在执行导致 Null 初始化的方法后加载配置值,然后它将返回正确的值。

I followed Microsoft documentation .我遵循了 Microsoft 文档

Appseting File:应用程序文件:

wwwroot/appsettings.json

My Component.razor:我的组件.razor:

@page "/"

<h1>Configuration example</h1>

<p>Message: @Configuration["message"]</p>
@code {
   protected override async Task OnParametersSetAsync()
   {
      await trying to read Configuration["Message"];
    }
}

My Program.cs File:我的 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();
    }
}

My _Imports.razor我的 _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;

Note: Understand that I am getting the value's correctly but always null first then re-renders with correct values, i am using OnParametersSetAsync() as entry point, i understand OnParametersSetAsync() changes on every Parameter Change.注意:了解我得到的值是正确的,但总是先 null 然后用正确的值重新渲染,我使用 OnParametersSetAsync() 作为入口点,我理解 OnParametersSetAsync() 每次参数更改都会发生更改。 but the Configuration Values should be constant not null first right?但配置值应该是不变的,而不是 null 首先对吧?

I had the same problem today when I tried to fetch connection string value from appsettings.json.今天我尝试从 appsettings.json 获取连接字符串值时遇到了同样的问题。 Try to Restart Visual Studio and it will work properly.尝试重新启动 Visual Studio ,它将正常工作。

it worked after updating to blazor nuget packages from 3.2.0 to 3.2.1从 3.2.0 到 3.2.1 更新到 blazor nuget 包后它工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加载不同的 appsettings.json 到 Window object 在 Z98AD8B3C99B3CA16F1F7FA84EE64C44F 中 - Load different appsettings.json into Window object in Blazor WebAssembly Blazor WebAssembly - appsettings.json 作为注册服务时的依赖项 - Blazor WebAssembly - appsettings.json as dependency while registering services 在 Automapper 配置中使用 appsettings.json 值 - Using appsettings.json values in Automapper configuration .NET Core获取配置appsettings.json值 - .NET Core getting Configuration appsettings.json values 在 configuration.build 之前从 appsettings.json 读取值 - Read values from appsettings.json before configuration.build 如何从programs.cs Blazor应用程序中的配置中获取appsettings.json中设置的值 - How do I get the value of a setting in appsettings.json from configuration in programs.cs Blazor app 在 EF 上下文中使用 appsettings.json 配置 - Using appsettings.json configuration in EF Context 未找到配置文件“appsettings.json”且不是可选的 - The configuration file 'appsettings.json' was not found and is not optional appsettings.json 界面内的配置界面 - Configuration Interface inside an interface in appsettings.json 读取 appsettings.json - 字段保持为空 - Read appsettings.json - Fields remain null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM