aspnetcore
aspnetcore copied to clipboard
NavigationManager.NavigateTo throws exception when navigating in OnInitialized
Is there an existing issue for this?
- [X] I have searched the existing issues
Describe the bug
I have complex Blazor Server application.
Our .razor Pages inherit from our own BaseClass. In this Base class we are trying to load data during OnInitialized event.
When some data is not available, we need to stop loading the page (stop rendering) and redirect user to some other page.
However when we use NavigationManager in such scenario, it throws Microsoft.AspNetCore.Components.NavigationException: 'Exception_WasThrown'
If the loading/rendering of the page continues, the applications eventually encounters some exceptions and returns 500 response to the user.
How can we prevent this? I need to be able to say to blazor during OnInitialized "stop whatever you are doing and navigate the user to this page instead". The navigation should be real request/response.
Expected Behavior
No response
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
No response
Anything else?
No response
Thanks for reaching out. A NavigationException
gets thrown when NavigationManager.NavigateTo()
gets called during static server rendering. This is by design. The exception is intended to be caught by the framework so that a redirect can be initiated.
If you know what's directly causing the 500, could you please elaborate on what that is? The NavigationException
alone shouldn't be causing a 500, so we anticipate that there's some additional logic in your app that might be causing it.
If possible, could you please also provide us with a minimal repro project that demonstrates the issue? This will help us investigate this further.
Thanks for reaching out. A
NavigationException
gets thrown whenNavigationManager.NavigateTo()
gets called during static server rendering. This is by design. The exception is intended to be caught by the framework so that a redirect can be initiated.If you know what's directly causing the 500, could you please elaborate on what that is? The
NavigationException
alone shouldn't be causing a 500, so we anticipate that there's some additional logic in your app that might be causing it.If possible, could you please also provide us with a minimal repro project that demonstrates the issue? This will help us investigate this further.
The blazor page (e.g. "page.razor"
inherits from "pages_base.cs"
parent). They both (page and its parent) have OnInitialized
method. the page calls "base.OnInitialized"
at the start of it's OnInitialized
method.
The problem is that in the parent (pages_base.cs) OnInitialized
I figure we don't have all the data and would need to stop everything and navigate away. But even when I navigate and stop the OnInitialized by "return" from the method, it then continues by executing the child (page.razor) OnInitialized and there happens the exception bc some of the data is null, but the page tries to work with it.
This stops the navigation that I initiated in the parent (pages_base.cs) OnInitialized
method and instead the application returns HTTP 500 error because of the Exception in the child class. I could of course handle it in every child class, but we have really many of these so I would very much appreciate some solution where the navigation can happen in the parent class.
What I would need is something like "completely stop everything, exit rendering and navigate to new page with real http request" in the parent before the child continues with its own OnInitialized.
Is this possible?
Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project that illustrates the problem.
Thank you for filing this issue. In order for us to investigate this issue, please provide a minimal repro project that illustrates the problem without unnecessary code. Please share with us in a public GitHub repo because we cannot open ZIP attachments, and don't include any confidential content.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
See our Issue Management Policies for more information.