Blazor component : refresh parent when model is updated from child component

Create a shared service. Subscribe to the service’s RefreshRequested event in the parent and Invoke() from the child. In the parent method call StateHasChanged(); public interface IMyService { event Action RefreshRequested; void CallRequestRefresh(); } public class MyService: IMyService { public event Action RefreshRequested; public void CallRequestRefresh() { RefreshRequested?.Invoke(); } } //child component MyService.CallRequestRefresh(); //parent component … Read more

StateHasChanged() vs InvokeAsync(StateHasChanged) in Blazor

I have tried calling StateHasChanged() – instead of InvokeAsync(StateHasChanged) – in a Timer’s Elapsed event, and it works as expected That must have been on WebAssembly. When you try that on Blazor Serverside I would expect an exception. StateHasChanged() checks if it runs on the right thread. The core issue is that the rendering and … Read more

Get Current User in a Blazor component

There are three possibilities for getting the user in a component (a page is a component): Inject IHttpContextAccessor and from it access HttpContext and then User; need to register IHttpContextAccessor in Startup.ConfigureServices, normally using AddHttpContextAccessor. Edit: according to the Microsoft docs you must not do this for security reasons. Inject an AuthenticationStateProvider property, call GetAuthenticationStateAsync … Read more

tech