Reputation: 65
In Blazor server the "appsettings.json" file is great for storing globally accessible variables. But what if these need changing during runtime? For example, lets say we have a stored value for "IsMaintenanceMode".
Given that;
"IsMaintenanceMode" may need to be set to "True" during runtime (to then direct users to a maintenance page)
If we were using a middleware to check this value for True (i.e. redirect the user to maintenance page) - then we would not want to look this variable up each time - eg from a database - on every request.
Traditionally this might have been accomplished using Application variables but I'm just not sure of the best approach with Blazor.
So my question is - what's the best way of storing this value in a way that can be "cached" for ease of lookup, but also easily changed during runtime?
Thanks for any advice.
Upvotes: 0
Views: 1137
Reputation: 4236
StateServer.cs
public class StateServer {
public bool IsMaintenanceMode {get; set;}
}
Add it in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<StateServer >();
}
Component.razor // Can be layout, main page, etc.
@inject StateServer _stateServer
@if (_stateServer.IsMaintenanceMode){
<Warning />
}
else {
<Body />
}
@code {
}
Or, you can check the value in one of the page lifecyle events, and navigate to whatever page you like.
Upvotes: 1