dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.5k stars 10.04k forks source link

Blazor: NavigationManager - Prevent navigation #15428

Closed mrpmorris closed 5 years ago

mrpmorris commented 5 years ago

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

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 commented 5 years ago

I think this is related to #14962

MV10 commented 5 years ago

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 commented 5 years ago

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.

mrpmorris commented 5 years ago

Duplicate of #14962