-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
.NET 8 Blazor - NavigationManager.NavigateTo() Inconsistent Behavior #53996
Comments
@sbwalker thanks for filing an issue. This has come up in the past, and we discussed fixing it for 9.0. I don't know if we have an issue tracking it, so I'm going to choose this one as the one, but in general, we all agree that the current way this works is not great. |
@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? |
I'm having a similar issue with the NavigationManager.NavigateTo() method. My use case is pretty simple but a lot of functionality in the app relies on being able to handle routing parameters to initialize data in the app and is no longer working as it used to.
The idea is that in my RedirectToLogin component a non-authorized user would be redirected to a login and then directed to their URL so it can be decoded and initialized. But currently it doesn't seem that the returnUrl is working as expected. Instead of navigating to the original URL it just forgets it and navigates to the base URL of the app. The app is newly updated for Dotnet 8 from Dotnet 6 and this seems to be the only major issue. Using package versions 8.0.2. Potentially related I found that I had to add
to my csproj because of a different authentication redirect issue so maybe there are some issues with this package? |
I encounter a similar issue whenever I run my code with a debugger. It functions properly without a debugger. |
@sherviniv Are you actually encountering an issue, or simply an exception? |
@KennethHoff In both cases(without a debugger or with it) it is working. But with the debugger mode, I got an exception. |
@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. |
The Blazor Web template with authentication enabled includes an IdentityRedirectManager class which appears to provide a workaround for this issue (ie. so that exceptions are not thrown): // During static rendering, NavigateTo throws a NavigationException which is handled by the framework as a redirect.
|
A RedirectTo method for the Blazor SSR without any exception! public static class BlazorSsrRedirectManagerExtensions
{
public static void RedirectTo(this HttpContext httpContext, string redirectionUrl)
{
ArgumentNullException.ThrowIfNull(httpContext);
httpContext.Response.Headers.Append("blazor-enhanced-nav-redirect-location", redirectionUrl);
httpContext.Response.StatusCode = 200;
}
} |
Doesnt work |
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): @code{
[CascadingParameter] public HttpContext? HttpContext { get; set; }
} |
Tried using the the CascadingParameter also, If I call RedirectTo in
Not sure what I am doing wrong. |
Make it an enhanced form, it will work (it's a blazor-enhanced-nav-redirect-location): <EditForm FormName="AddOrder" OnValidSubmit="SubmitOrder" Model="InputModel" method="post" Enhance> |
That worked, thank you! |
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 |
In my case, this error leads to permanent errors. It does not help to build a TRY CATCH around it, as the redirect is not executed. It would be really great if this error could finally be fixed, because this is used consistently in my application. |
@VahidN In my Log in form, the It didn't work in the Log out form, but |
I was stuck for multiple days on this issue, and you managed to make it work, thank you very much for your help and your code. Now I'm going to rant a little, it would be great if we had more informations in the exception to know what happen behind the scene. It's not only the NavigationManager, Blazor is full of quirky stuff like this, and I'm forced to use it, it's not pleasant I'll contribute in the future when I can to fix those issues |
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. |
Calling inside of try...catch block will prevent the Calling outside of try...catch block will ignore the Exception and Although, this is not a normal behaviour of the |
That's why you shouldn't use |
So.... will this be fixed in .NET 9.0? I have also spend quite a few hours on this issue and it seems that the problem has been around for a few years. It is indeed very discouraging to deal with these kinds of issues. The same goes for buttons in SSR. I understand the fundamental problem, but come on. |
Absolutely not in 9.0; if it's not in by July/August, it won't be in the upcoming version. |
Oy. Then how do I deal with this correctly? Because currently I am simply getting an exception after which I press continue in my Visual Studio. Which is bad for demos. |
When it throws the exception, there's a box you can uncheck for 'break when this exception type is thrown'. Uncheck that or in exception settings in VS. |
Aha, so that is the actual solution to this bug. And we are all sure that this has no issues in production? In that case thank you for clearly stating this as the actual solution (Y) |
@Woudjee, the solution provided by @VahidN worked for me. (It didn't work for my Log out form, but From earlier in this thread: A public static class BlazorSsrRedirectManagerExtensions
{
public static void RedirectTo(this HttpContext httpContext, string redirectionUrl)
{
ArgumentNullException.ThrowIfNull(httpContext);
httpContext.Response.Headers.Append("blazor-enhanced-nav-redirect-location", redirectionUrl);
httpContext.Response.StatusCode = 200;
}
} You should define the @code
{
[CascadingParameter] public HttpContext? HttpContext { get; set; }
} |
Thank you! This is very clear 😁😎👍 I will try it out next week! |
Tip! await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, cookieClaims,
new AuthenticationProperties
{
// ...
RedirectUri = UriHelper.Encode(new Uri(ReturnUrl))
});
|
This issue (and many others) were covered in this Black Belt Blazor presentation: |
Some of the issues here will be addressed by #58573. |
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? |
@erossini |
Issue only found while debug mode (just continue debug it works). In release mode no errors. |
Is there an existing issue for this?
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:
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
The text was updated successfully, but these errors were encountered: