Skip to content

Blazor: NavigationManager - Prevent navigation #15428

Closed
@mrpmorris

Description

@mrpmorris

I am trying to put a guard condition on navigating to a page before the navigation is attempted... i.e. before Blazor even tries to determine if the page exists.

My requirement is as follows
1: Navigation is attempted
2: My code checks if an Assembly has been loaded from the server
3: If so, then continue
4: If not, then download the assembly and then navigate to the URL requested

Other requirements could be as follows

  • Show a dialog asking the user if they wish to save their changes
  • Show a page fade-out transition

Describe the solution you'd like

1: Change void NavigateTo to Task<bool> NavigateToAsync
2: Add a method IDisposable AddNavigationGuard(Task<bool> guard, int? order = null)

The coder can call Dispose on the result of that to remove the guard.

Before navigating, the NavigationManager should iterate over each guard, and await the result. If the result of any individual guard is false then the iteration should stop and NavigateToAsync should return false.

Summary

This approach would let us hook in additional logic before navigating to a page is performed.

This would allow me to create a global hook to download a DLL module before Blazor decides if the page exists or not. It would also allow developers to have in-page hooks to confirm navigation before changes are lost by using a nice async approach such as a dialog - where the registered hooks in the current page can simply be disposed of in MyComponent.Dispose.

Metadata

Metadata

Labels

area-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing one

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions