1

I'm developing a mandate based application where i want to load the "context" data like the mandate name, user information like name, language and other stuff before the user sees the app.

Where would be the place to do this? If i load the data in OnInitializedAsync of App.razor, the data is only present after the app is already visible. If i set the Culture based on the received language, the components are already rendered with the wrong culture. Every tutorial mentions the Program class as the place to set the Culture to be there from the start. Is there a way to make and await this WebApi-call and populate this data at a point where setting the Culture has an impact on the initial render?

1
  • Why don't you use some of of version of the standard "loading" setup where you display some sort of loader message/icon/spinner until all the relevant stuff is loaded and set up? Commented Oct 22, 2021 at 17:19

1 Answer 1

2

Put this in the OnInitializedAsync() method of your MainLayout.razor

Your code can look something like this

MainLayout.razor

<CascadingParameter Value="@mandate">
<div class="p-4">
 @if(loading)
 {
    <p> Loading .... </p>
   
 }else
 {
    @Body
 }
</div>
</CascadingParameter>

@code
{
   Mandate mandate; // Assuming some Mandate class which holds information
   bool loading;
   protected override Task OnInitializedAsync()
   {
     loading = true;
     mandate = await LoadMandate(); /// load preferences from the database etc 
     loading = false;
   }

}

Ideally, push this part to a service and load it there.

Sign up to request clarification or add additional context in comments.

2 Comments

While i like the idea for general data, it does not work with the language. If i put the following in OnInitialized of the main component (App.razor in my case), IStringLocalizer is using the wrong language in all components. If i put those lines in Program.cs before RunAsync, it works: CultureInfo.CurrentCulture = "fr-CH"; CultureInfo.CurrentUICulture = "fr-CH"; CultureInfo.DefaultThreadCurrentCulture = "fr-CH"; CultureInfo.DefaultThreadCurrentUICulture = "fr-CH";
For localization specific issues, can you not use this? learn.microsoft.com/en-us/aspnet/core/blazor/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.