aspnetcore: .NET 8 Blazor - NavigationManager.NavigateTo() Inconsistent Behavior
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
A very common use case in web applications is that the user may need to navigate to another Url. Usually this is accomplished with a standard hyperlink - however there are often scenarios where you need to do some validation or processing prior to redirecting the user.
In Interactive render mode you can use an @onclick handler and call NavigationManager.NavigateTo(). However if you are creating components which support Static render mode, the @onclick handler approach does not work so you need to use standard forms and button instead.
The component code below includes a simple form with a button which performs a redirect:
@inject NavigationManager NavigationManager
<form method="post" @onsubmit="Submit" @formname="MyUniqueFormName">
<AntiforgeryToken />
<button type="submit" class="btn btn-primary">Redirect</button>
</form>
@code {
private void Submit()
{
NavigationManager.NavigateTo("/");
}
}
When running this code on Interactive render mode it works fine… it redirects the user to the “/” path.
However when running this code on Static render mode, it throws an exception:
Similar behavior related to NavigationManager has been reported previously (ie. #13582) however the recommended solution was to instruct the developer to ignore the exception in their Visual Studio IDE:
This is not a good resolution, as most developers would not have this exception ignored by default, and most developers would assume it is a problem with the Blazor application code.
Expected Behavior
Expected behavior would be for the NavigationManager.NavigateTo() method to behave consistently on Static and Interactive render modes ie. the user would be redirected and no run-time exception would occur.
Alternatively, some official guidance from Microsoft on how to deal with this scenario in Blazor applications would be useful. For example if the expectation is that developers need to create their own wrapper class around NavigationManager.NavigateTo() to handle the exception in Static rendering, then it should be included in the documentation.
Steps To Reproduce
The code in the issue reproduces the issue… just be sure to instantiate the component in Interactive render mode.
Exceptions (if any)
No response
.NET Version
8.0.1
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 15
- Comments: 42 (7 by maintainers)
Links to this issue
Commits related to this issue
- 🐛 NavigationManager.NavigateTo() Inconsistent Behavior https://github.com/dotnet/aspnetcore/issues/53996 — committed to neozhu/CleanArchitectureWithBlazorServer by neozhu 2 years ago
Even though the exception is thrown and the app works properly, many people spend hours trying to figure out what they have done wrong, which can be very confusing. This error is quite common. Many Blazor developers have switched from Angular, React, and when they realize that they have spent hours on a bug that wasn’t their fault, they may feel discouraged and reconsider migrating to Blazor.
I’m facing the same issue with the navigation. I’m creating a new project with Blazor on .NET 9. In this project, I want to add the Authentication Type as “Individual Accounts”. For that, I create a new project with the following values:
The project was created successfully and I ran the update-database to create the ASP.NET database. When I run the application and register a new user, I get this error:
I don’t understand how to avoid this error. Is there any workaround?
A RedirectTo method for the Blazor SSR without any exception!
We are tracking improvements to this experience https://github.com/dotnet/aspnetcore/issues/59451
This issue (and many others) were covered in this Black Belt Blazor presentation:
https://youtu.be/1lsjpfdqBk0?si=IJNe3IkdjJubcLbe
Make it an enhanced form, it will work (it’s a blazor-enhanced-nav-redirect-location):
You should define the HttpContext for a Blazor SSR page this way and call that method at the end of the processed request (it will not end the request immediately):
Tip! How to redirect from the
Loginpage in Blazor SSR app’s without using theNavigationManager:RedirectUrishould be properly encoded for use in HTTP headers by using the UriHelper.Encode method.I spun up a Blazor Web App for .NET 9, Individual Accounts, Auto (Server and WebAssembly), and Per page/component, and included sample pages. Works fine, but when registering an account it throws an exception as well, is this expected? Any solution other than configuring the VS to ignore it? This seems like an issue that is going to result in a flood of issues when people start adopting it
@javiercn I just realized that catching and ignoring the exception in your application code is not a solution, as the NavigationManager will fail to redirect to the desired location.
Created an extension method
and used it in my component:
The exception is ignored - but no navigation occurs - the existing UI will continue to be displayed.
So it seems that the only way to deal with this issue in .NET 8 is to configure the Visual Studio dev tools to ignore the exception - and then it will work as expected.
@KennethHoff In both cases(without a debugger or with it) it is working. But with the debugger mode, I got an exception.
I encounter a similar issue whenever I run my code with a debugger. It functions properly without a debugger.
Microsoft.AspNetCore.Components.NavigationException HResult=0x80131500 Message=Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown. Source=Microsoft.AspNetCore.Components.Server StackTrace: at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, NavigationOptions options) at Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(String uri, Boolean forceLoad) at RedirectToLogin.OnInitialized() in RedirectToLogin.razor:line 6 at Microsoft.AspNetCore.Components.ComponentBase.<RunInitAndSetParametersAsync>d__21.MoveNext()@javiercn I am glad we are in agreement 😃 In the interim (before .NET 9) is the recommendation to create a simple wrapper around NavigationManager.NavigateTo() which traps and ignores the exception when running on static render mode?