Getting settings from appsettings.json to afterStarted(options) in Blazor | Blazor Server

Viewed 94
1 Answers

Maybe inject IConfiguration into the blazor page?

@inject IConfiguration _configuration


protected override afterStarted(){
    var somevariable = _configuration["othervariable"];
}

Failing that, I create an object like ApplicationState with an interface, make that a scoped service in Program.cs, define the object's properties, then inject that interface into your blazor page:

Program.cs

builder.Services.AddScoped<IApplicationState, ApplicationState>();
var applicationState = app.Services.GetRequiredService<IApplicationState>();
applicationState.someVar = builder.Configuration["SomeVar"];

blazor

@inject IApplicationState _applicationState

protected override afterStarted(){
    var somevariable = _applicationState.someVar;
}

Hope this helps

Related