Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blazor: NavigationManager - Prevent navigation #15428

Closed
mrpmorris opened this issue Oct 26, 2019 · 4 comments
Closed

Blazor: NavigationManager - Prevent navigation #15428

mrpmorris opened this issue Oct 26, 2019 · 4 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one
Milestone

Comments

@mrpmorris
Copy link

mrpmorris commented Oct 26, 2019

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.

@Postlagerkarte
Copy link

I think this is related to #14962

@mrpmorris mrpmorris changed the title Blazor: NavigationManager guard Blazor: NavigationManager - Prevent navigation Oct 26, 2019
@MV10
Copy link

MV10 commented Oct 26, 2019

I have cross-cutting use-cases for this, too: parent components with state that navigation-producing child components are unaware of. I was a little surprised there isn't a general Cancel method on the System.EventArgs base class.

@mrpmorris
Copy link
Author

I've added a pull request, but cancelling it because it's not the way it should be done. I will update the issue to explain why.

@blowdart blowdart added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added question enhancement This issue represents an ask for new feature or an enhancement to an existing one and removed question labels Oct 28, 2019
@mkArtakMSFT mkArtakMSFT added this to the Backlog milestone Oct 28, 2019
@mrpmorris
Copy link
Author

Duplicate of #14962

@ghost ghost locked as resolved and limited conversation to collaborators Dec 2, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components enhancement This issue represents an ask for new feature or an enhancement to an existing one
Projects
None yet
Development

No branches or pull requests

6 participants