Skip to content
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

NavigationManager.NavigateTo doesn't navigate in Blazor Web App #49142

Closed
danroth27 opened this issue Jul 2, 2023 · 1 comment · Fixed by #49768
Closed

NavigationManager.NavigateTo doesn't navigate in Blazor Web App #49142

danroth27 opened this issue Jul 2, 2023 · 1 comment · Fixed by #49768
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-full-stack-web-ui Full stack web UI with Blazor
Milestone

Comments

@danroth27
Copy link
Member

Repro steps:

  • Create a Blazor Web App with server interactivity enabled
  • Update Index.razor with:
@page "/"
@inject NavigationManager NavigationManager
@attribute [RenderModeServer]
<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<button @onclick="Navigate">Navigate</button>

@code {
    void Navigate()
    {
        Console.WriteLine("Navigate");
        NavigationManager.NavigateTo("/counter");
    }
}
  • Run the app and click the Navigate button

Expected result: App navigates to the Counter
Actual result: Browser address updates, but the counter isn't rendered:

msedge_SocAQubGpE

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jul 2, 2023
@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Jul 3, 2023

Good report. What's happening is that because the source page is interactive but has no interactive router,

  • Your .NET code issues a programmatic navigation, which results in a call to JS telling to navigate
  • The JS code begins the navigation flow by updating the URL
  • Next it sees that you're navigating to an internal URL, so it doesn't try to leave the page, but instead issues a call into .NET to raise the OnLocationChanged/OnLocationChanging events
  • Normally those events would be received by a <Router> which would match against its route table, and if there's no match, would trigger a full-page load to the destination URL
  • However in this case you have no <Router> so the OnLocationChanged/OnLocationChanging events are just ignored

It's slightly awkward because in theory, the developer might have some custom code that listens to OnLocationChanged/OnLocationChanging to perform arbitrary actions in this case, so we don't know for sure that any of this flow is actually wrong. Maybe you wanted to take no action on these events!

I think the best resolution here will be for us to add an extra condition that the JS-side code only attempts to notify .NET (and to cause OnLocationChanged/OnLocationChanging) when navigation interception has been enabled, which is something that <Router> does. The JS side navigation code already varies its behavior in quite a few ways based on whether there's a navigation interceptor, i.e., an interactive router.

At first I thought this could as breaking change as now you'd only be able to use OnLocationChanged/OnLocationChanging if you have either a <Router> or a custom router that uses enableNavigationInterception. However OnLocationChanged/OnLocationChanging already only react to navigation clicks and popstate if you use enableNavigationInterception, so the only case that would change is NavigationManager.NavigateTo, and it's hard to see a legit reason why that should differ from link clicks and back/forward. As such I think it's better regarded as a bugfix.

@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Jul 3, 2023
@mkArtakMSFT mkArtakMSFT added this to the 8.0-preview7 milestone Jul 3, 2023
@mkArtakMSFT mkArtakMSFT modified the milestones: 8.0-preview7, 8.0-rc1 Jul 14, 2023
@mkArtakMSFT mkArtakMSFT added feature-full-stack-web-ui Full stack web UI with Blazor help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team labels Jul 14, 2023
@mkArtakMSFT mkArtakMSFT removed the help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team label Jul 31, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug. feature-full-stack-web-ui Full stack web UI with Blazor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants