What is the LocationChanged event on NavigationManager, and how do you subscribe to and clean up from it?

Last updated 5 July 2026

  • LocationChanged is a NavigationManager event that fires after the URL has changed, so it can react (logging, resetting state) but cannot prevent navigation.

  • Subscribe with += in OnInitialized and unsubscribe with -= in Dispose (implement IDisposable).

  • Cleanup matters because the long-lived NavigationManager would 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/
All Blazor interview questions