What is the LocationChanged event on NavigationManager, and how do you subscribe to and clean up from it?
Last updated
LocationChangedis aNavigationManagerevent that fires after the URL has changed, so it can react (logging, resetting state) but cannot prevent navigation.Subscribe with
+=inOnInitializedand unsubscribe with-=inDispose(implementIDisposable).Cleanup matters because the long-lived
NavigationManagerwould otherwise keep the component alive, causing a memory leak.
- https://learn.microsoft.com/en-us/answers/questions/1693004/blazor-server-oidc-logout-with-keycloak-redirects
- https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/navigation?view=aspnetcore-10.0
- https://www.telerik.com/blogs/blazor-basics-9-best-practices-building-blazor-web-applications
- https://crispycode.net/blazor-cheat-sheet/