-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
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