Skip to content

NavigationException when attempting to navigate after submitting a form #49143

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

Closed
danroth27 opened this issue Jul 2, 2023 · 4 comments
Closed
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

danroth27 commented Jul 2, 2023

Repro steps:

  • Create a Blazor Web App with .NET 8 Preview 6
  • Update Index.razor with the following:
@page "/"
@inject NavigationManager NavigationManager

<PageTitle>Index</PageTitle>

<h1>Hello, world!</h1>

Welcome to your new app.

<EditForm method="post" Model="new()" OnValidSubmit="Navigate">
    <button type="submit">Submit and navigate</button>
</EditForm>

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

Expected result: Form is submitted and app navigates to the counter page
Actual result:

Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
   at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, NavigationOptions options)
   at Microsoft.AspNetCore.Components.NavigationManager.NavigateToCore(String uri, Boolean forceLoad)
   at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad, Boolean replace)
   at BlazorApp3.Pages.Index.Navigate() in C:\Users\daroth\source\repos\BlazorApp3\BlazorApp3\Pages\Index.razor:line 18
   at Microsoft.AspNetCore.Components.EventCallbackWorkItem.InvokeAsync[T](MulticastDelegate delegate, T arg)
   at Microsoft.AspNetCore.Components.ComponentBase.Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(EventCallbackWorkItem callback, Object arg)
   at Microsoft.AspNetCore.Components.EventCallback`1.InvokeAsync(TValue arg)
   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
   at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore()
   at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore()
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<<InvokeAsync>b__9_0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Host: localhost:7108
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.51
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryc1xzNe1hpeP893LF
Cookie: .AspNetCore.Antiforgery.IeH0rP6rx9A=CfDJ8F4wj4CPUDJJpKiv_dArKQq7-zlTyLTppJPATs2r_sPqwv8mRiR6HIH34f0IqKK0Ah0Xrt_EqvXLOj0sR0epDhOMweYTt-TA4ZXGW_plxoQ85aIu_hmmOp-ypV-fltkIUoDYZ6TBl74EW4yHbVUfF54
Origin: https://localhost:7108
Referer: https://localhost:7108/
Content-Length: 44
sec-ch-ua: "Not.A/Brand";v="8", "Chromium";v="114", "Microsoft Edge";v="114"
sec-ch-ua-mobile: ?0
blazor-enhanced-nav: on
sec-ch-ua-platform: "Windows"
sec-fetch-site: same-origin
sec-fetch-mode: cors
sec-fetch-dest: empty
@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Jul 2, 2023
@javiercn
Copy link
Member

javiercn commented Jul 3, 2023

This is happening because the Blazor Server navigation manager is overriding the one for endpoints RemoteNavigationManager

@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Jul 3, 2023

Are you sure? It doesn't look like there even is a Blazor Server navigation manager in this case.

I think what's happening is that DispatchCapturedEvent runs at the wrong time, for two reasons:

  1. It's not wrapped in any try/catch(NavigationException) hence the immediate problem here
  2. It runs after we've reached quiescence, but if it causes any async work, then it will sets quiesceTask to an incomplete task. This means the renderer goes into the streaming SSR code paths even if streaming SSR is not enabled, which would be a bug.

Instead I think DispatchCapturedEvent should be triggered inside EndpointHtmlRenderer's RenderEndpointComponent, right after await WaitForResultReady. At this point, the outer code doesn't yet consider the renderer quiescent, so DispatchCapturedEvent is free to add other async work which will be considered part of the initial page render (without causing streaming SSR to start), plus it will automatically be inside the existing catch for NavigationException so that would just work.

@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
@danroth27
Copy link
Member Author

@javiercn @SteveSandersonMS Are there any workarounds for this issue with the current bits?

@SteveSandersonMS
Copy link
Member

Done in #49472

@ghost ghost locked as resolved and limited conversation to collaborators Aug 17, 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

No branches or pull requests

4 participants