What is the purpose of NavigationManager, and how do you intercept navigation to prevent a user from leaving a dirty form?
Last updated
NavigationManageris an injected service for URLs: readUri/BaseUri, navigate withNavigateTo(options likeforceLoad,replace), and observe location changes.To guard a dirty form (.NET 7+), call
RegisterLocationChangingHandlerand invokecontext.PreventNavigation()when the user declines to leave.Dispose the returned registration to unsubscribe.
NavigationLockwraps 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