Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i want to set variable (someFlag = false/true) in JS so i can disable/enable javascript function.

I would like to do it with afterStarted(options) [described in link below].

https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-6.0#javascript-initializers

The question is:

Is there any way to get the data from appsettings.json to function afterStarted(...). Either through options parameter or something else, doesn't matter.

Thanks.

like image 886
Tomáš Zálešák Avatar asked Dec 05 '25 10:12

Tomáš Zálešák


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

like image 197
Liam Kenny Avatar answered Dec 07 '25 23:12

Liam Kenny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!