What is the purpose of NavigationManager, and how do you intercept navigation to prevent a user from leaving a dirty form?

Last updated 5 July 2026

  • NavigationManager is an injected service for URLs: read Uri/BaseUri, navigate with NavigateTo (options like forceLoad, replace), and observe location changes.

  • To guard a dirty form (.NET 7+), call RegisterLocationChangingHandler and invoke context.PreventNavigation() when the user declines to leave.

  • Dispose the returned registration to unsubscribe. NavigationLock wraps the same mechanism declaratively.

  • https://www.telerik.com/blogs/blazor-new-locationchanging-events-dotnet-7
  • https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/navigation?view=aspnetcore-10.0
  • https://medium.com/@@mariekie/blazor-features-in-net-7-c6ddf6de0107
  • https://www.youtube.com/watch?v=UkHezqeIQUE
  • https://dev.to/shauncurtis/controlling-routing-and-navigation-in-blazor-edit-forms-bmh
  • https://blazor-university.com/routing/detecting-navigation-events/
  • https://www.telerik.com/blazor-ui/documentation/knowledge-base/form-prompt-unsaved-changes
All Blazor interview questions