Expose location changing event for NavigationManger #14962
Comments
Thank you for your feature request @Postlagerkarte. We'll consider this feature during the next release planning period and update the status of this issue accordingly. |
Not sure if this is what the OP was after with LocationChanging but it would be nice to have LocationChangeStart and LocationChangeEnd events. The use case for me would be to display a little progress bar to the user while navigation is happening. |
It would be nice to cancel the navigation to. Use Case is that the user has unsaved changes and show an error to him so he is not leaving without save. |
I hope that the team decides to give us a full set of methods, properties, and events to support navigation including easy access to the navigation history. Such an improved version of the NavigationManager would certainly allow tracking the lifetime of a navigation via events like:
Basically, hoping for an improved/adjusted version of the NavigationService |
Yes, it's great being able to develop an SPA application, but when the user can navigate away with changes unsaved, and no way to warn them, that's a serious flaw. You can use the javascript window.history.forward() trick in the _Hosts.html file to force the user back, but that refreshes the page losing all unsaved changes. For critical editing, I'm starting to use a state service to preserve changes, but it's a bit of a cludge to get round the problem. |
If anyone can post suggestions on how best to implement this in SSB without waiting for Microsoft, please do so here. |
And would be nice to have a way to handle history stack or just a simple "location.replace(url);"
then call: I mean something like this, so more practical: |
@lucianotres We are adding that in a future update I think, but you don't need anything special to do that today, you can simply call |
Are there any news about this issue's status? |
Hi,
The key here is to get your head around what's actually going on, and what the various classes/objects are up to. With this approach I no longer need to tamper with the back/forward history - the router handles "In Application" page changes and onbeforeunload warns the user if they are trying to navigate away. Note that creating a customized version of the router isn't trivial. You will need to make copies of various other classes that are namespace restricted. I'll put a post here when I have something polished enough to publish. |
@ShaunCurtis thanks for reply! |
@ShaunCurtis struggling with step 4 cannot wrap my head around it is there any other way right now? is solution for this still in progress? |
I'm currently working on putting a working example together - literally as I write this. Hopefully I'll have something in the next day or two. This will include the "unwrapped" router code. |
I've now published the routing on GitHub with a working example project. You can install the DLL via a Nuget Package (I hope!). The source code is all there in the GitHub repository. |
I've added a further project - CEC.FormControls - that uses the router with a more polished editor component similar to what I've been using in my projects. It uses enhanced Blazor InputBase Controls and the EditContext to manage editor control and routing. The Github repository is here: |
I just put |
Hi @ShaunCurtis. Thanks! Will this library work for web assembly project as well? I need to rewrite URL to redirect URL to always remove or always add a port. For example: localhost:5001/authorize to localhost/authorize. Is this possible with your library? |
Hi Nemtajo, |
I found a much simpler method, In Startup.cs of the server project I added:
I used this as a reference: //https://stackoverflow.com/questions/51105799/how-do-i-force-https-redirect-on-static-files-in-asp-net-core-2-1 |
Thanks for contacting us. |
If a User clicks the back button on the browser on a path that shouldn't be allowed (where the back goes to a place within the same app so no full navigation) would this allow for some form of breaking that navigation? The use case is when you have gone through a wizard, and once completed it's not ok to go back to the previous screen. You can handle this in the app but its a lot more code to handle all the instances of where I was and how I got to here type logic. If we could simple have a way to say use cannot go back or similar then you could block the user from trying to go to an unsupported location. If this doesn't sound like a good way to handle this type of situation, how would you recommend handling it? Using a UI to show an error if the navigation history wasn't in the right order? |
As a further add on to my other musings on this subject, I've worked on a slightly less radical approach that dumping the router. Sometimes being a bit revolutionary helps you think a problem through more logically! There's an article here : A-Blazor-Modal-Dialog-Editor To summarise, normal routing, but all editing/user entry wizard stuff in a Modal Dialog that locks off the rest of the screen. you still need to turn on/off the browser are you sure, but that's a issue for all SPAs. |
Thanks for contacting us. |
Thanks for contacting us. We're moving this issue to the |
If I am reading this correctly, this issue is not going to addressed in .net 6? |
@GiffenGood unfortunately, you read correctly. It puzzles me though since this is one of the most requested festures in my opinion. |
@smartprogrammer93 This is just so disappointing. We have been waiting for this feature forever and it is just a fundamental feature for a spa. What user wants to use spa where an accidental click or keypress can result in loss of work? |
@GiffenGood i mean there is a workaround, but you will have to copy the blazor router code from the repo and a bunch of other classes (since they sealed) just to create such a function. |
We were also waiting for this, to finally fix some user requests... @smartprogrammer93 do you have a link/repo to an working workaround? |
The NavigationManager exposes
LocationChanged | An event that fires when the navigation location has changed.
but it does not expose a
LocationChanging
event.Such an event that occurs before navigation takes place is very useful because it allows the previous view to prepare for its deactivation, for example, to display a warning, cancel navigation or silently save changes.
Can you provide such a feature or is there already another way to achieve the above?
The text was updated successfully, but these errors were encountered: