dotnet / dotnet/AspNetCore.Docs
Blazor Movie Database app tutorial - Part 4 concurrency sample is missing a `return` after `NavigationManager.NotFound()`
- Dominant language
- C#
- Stars
- 13.1k
- Forks
- 24.6k
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 109
Description
### 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
`true`.
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:
```csharp
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](https://github.com/dotnet/AspNetCore.Docs/issues?q=is%3Aissue+is%3Aopen+ce694ba5-636a-fe1b-dd40-694ec3489d06)
Contributor guide
Assessment
This issue has not been assessed yet.