Skip to content

.NET 8 Blazor - NavigationManager.NavigateTo() Inconsistent Behavior #53996

@sbwalker

Description

@sbwalker

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:

image

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:

64161981-6fc13680-ce36-11e9-8a77-5d243305cf7c

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Componentsbreaking-changeThis issue / pr will introduce a breaking change, when resolved / merged.enhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-navigationtriaged

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions