Skip to content

Blazor Movie Database app tutorial - Part 4 concurrency sample is missing a return after NavigationManager.NotFound() #37581

Description

@shaunplumb-sue

Description

The UpdateMovie sample assumes that NavigationManager.NotFound() ends the
method. It doesn't always. The framework halts execution by throwing a
NavigationException, and .NET 10 lets an app opt out of that with
<BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException>.
When the throw is disabled, execution leaves the catch block and reaches
NavigationManager.NavigateTo("/movies") on a response whose status code is
already 404.

Following the four test steps in that section, step 4 says the browser is
navigated to the Not Found page with a 404 status code. I got no Not Found page
and an unhandled exception instead. Stepping through it, MovieExists returns
false and the debugger reaches NotFound(), so the throw; branch is not
involved.

Suggested fix, which adds the return and moves the success redirect onto the
success path:

    try
    {
        await context.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (MovieExists(Movie!.Id))
        {
            throw;
        }

        NavigationManager.NotFound();
        return;
    }

    NavigationManager.NavigateTo("/movies");

The return makes the sample behave the same way whether or not the
NavigationException is thrown. It also stops the sample from teaching that a
thrown exception is the method's control flow, which is a surprising thing for a
beginner tutorial to model.

One other suggestion. The test steps could say that the Not Found content needs
somewhere to go. UseStatusCodePagesWithReExecute covers static SSR, and
Router.NotFoundPage is what applies once the components are interactive after
Part 8. With neither configured, NotFound() sets the status code and nothing
renders, which is easy to mistake for the sample being broken.

Page URL

https://learn.microsoft.com/en-gb/aspnet/core/blazor/tutorials/movie-database-app/part-4?view=aspnetcore-10.0&pivots=vsc

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/tutorials/movie-database-app/part-4.md

Document ID

ce694ba5-636a-fe1b-dd40-694ec3489d06

Platform Id

bd9facb3-c39a-53cb-f6bb-eeb8d81f7cbf

Article author

@guardrex

Metadata

  • ID: ce694ba5-636a-fe1b-dd40-694ec3489d06
  • PlatformId: bd9facb3-c39a-53cb-f6bb-eeb8d81f7cbf
  • Service: aspnet-core
  • Sub-service: blazor

Related Issues

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    P0/P1 - High Priority

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions