You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/tutorials/movie-database-app/part-1.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -299,6 +299,16 @@ if (!app.Environment.IsDevelopment())
299
299
}
300
300
```
301
301
302
+
:::moniker range=">= aspnetcore-10.0"
303
+
304
+
By default, an ASP.NET Core app doesn't provide a status code page for HTTP error status codes, such as *404 - Not Found*. When the app sets an HTTP 400-599 error status code without a body, it returns the status code and an empty response body. However, an app generated from the Blazor Web App project template calls <xref:Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.UseStatusCodePagesWithReExecute%2A> to add Status Code Pages Middleware to the request pipeline for pages that aren't found, which generates the response body by re-executing the request pipeline using the path to the Not Found error page (`/not-found`):
HTTPS Redirection Middleware (<xref:Microsoft.AspNetCore.Builder.HttpsPolicyBuilderExtensions.UseHttpsRedirection%2A>) enforces the HTTPS protocol by redirecting HTTP requests to HTTPS if an HTTPS port is available:
The `movie` variable is a private field of type `Movie`, which is a null-reference type (`?`), meaning that `movie` might be set to `null`.
421
447
422
448
The `Id` is a *component parameter* supplied from the component's query string due to the presence of the [`[SupplyParameterFromQuery]` attribute](xref:Microsoft.AspNetCore.Components.SupplyParameterFromQueryAttribute). If the identifier is missing, `Id` defaults to zero (`0`).
423
449
424
450
`OnInitializedAsync` is the first component lifecycle method that we've seen. This method is executed when the component loads. <xref:Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.FirstOrDefaultAsync%2A> is called on the database set (`DbSet<Movie>`) to retrieve the movie entity with an `Id` equal to the `Id` parameter that was set by the query string. If `movie` is `null`, <xref:Microsoft.AspNetCore.Components.NavigationManager.NavigateTo%2A?displayProperty=nameWithType> is used to navigate to a `notfound` endpoint.
425
451
426
-
<!-- UPDATE 10.0 - Update for scaffolder changes when they
The .NET scaffolder currently doesn't implement .NET 10's Not Found feature, but it can be implemented manually.
432
-
433
-
Update the line that navigates to a non-existent `notfound` endpoint. Change the line to call <xref:Microsoft.AspNetCore.Components.NavigationManager.NotFound%2A?displayProperty=nameWithType>:
434
-
435
-
```diff
436
-
- NavigationManager.NavigateTo("notfound");
437
-
+ NavigationManager.NotFound();
438
-
```
439
-
440
-
When a movie isn't found, this line change results in rendering the `NotFound` component, which produces a Not Found page in the browser with a 404 (Not Found) status code.
454
+
When a movie isn't found, calling <xref:Microsoft.AspNetCore.Components.NavigationManager.NotFound%2A?displayProperty=nameWithType> renders the `NotFound` component, which produces a Not Found page in the browser with a 404 (Not Found) status code.
441
455
442
456
:::moniker-end
443
457
@@ -509,21 +523,42 @@ The `AddMovie` method:
509
523
*<xref:Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync%2A> is called on the database context to save the movie.
510
524
*<xref:Microsoft.AspNetCore.Components.NavigationManager> is used to return the user to the movies `Index` page.
> Although it isn't a concern for the app in this tutorial, binding form data to entity data models can be susceptible to overposting attacks. Additional information on this subject appears later in this article.
The .NET scaffolder currently doesn't implement .NET 10's Not Found feature, but it can be implemented manually.
567
-
568
-
Update the line that navigates to a non-existent `notfound` endpoint. Change the line to call <xref:Microsoft.AspNetCore.Components.NavigationManager.NotFound%2A?displayProperty=nameWithType>:
569
-
570
-
```diff
571
-
- NavigationManager.NavigateTo("notfound");
572
-
+ NavigationManager.NotFound();
573
-
```
574
-
575
-
When a movie isn't found, this line change results in rendering the `NotFound` component, which produces a Not Found page in the browser with a 404 (Not Found) status code.
598
+
When a movie isn't found, calling <xref:Microsoft.AspNetCore.Components.NavigationManager.NotFound%2A?displayProperty=nameWithType> renders the `NotFound` component, which produces a Not Found page in the browser with a 404 (Not Found) status code.
576
599
577
600
:::moniker-end
578
601
@@ -597,6 +620,8 @@ The movie entity's identifier `Id` is stored in a hidden field of the form:
The movie entity's <xref:Microsoft.EntityFrameworkCore.EntityState> is set to <xref:Microsoft.EntityFrameworkCore.EntityState.Modified>, which signifies that the entity is tracked by the context, exists in the database, and that some or all of its property values are modified.
657
+
:::moniker-end
633
658
634
-
<!-- UPDATE 10.0 - Update for scaffolder changes when they
The .NET scaffolder currently doesn't implement .NET 10's Not Found feature, but it can be implemented manually.
667
+
try
668
+
{
669
+
awaitcontext.SaveChangesAsync();
670
+
}
671
+
catch (DbUpdateConcurrencyException)
672
+
{
673
+
if (!MovieExists(Movie!.Id))
674
+
{
675
+
NavigationManager.NavigateTo("notfound");
676
+
}
677
+
else
678
+
{
679
+
throw;
680
+
}
681
+
}
640
682
641
-
Update the line that navigates to a non-existent `notfound` endpoint. Change the line to call <xref:Microsoft.AspNetCore.Components.NavigationManager.NotFound%2A?displayProperty=nameWithType>:
683
+
NavigationManager.NavigateTo("/movies");
684
+
}
642
685
643
-
```diff
644
-
- NavigationManager.NavigateTo("notfound");
645
-
+ NavigationManager.NotFound();
686
+
privateboolMovieExists(intid)
687
+
{
688
+
usingvarcontext=DbFactory.CreateDbContext();
689
+
returncontext.Movie.Any(e=>e.Id==id);
690
+
}
646
691
```
647
692
648
-
When a movie isn't found, this line change results in rendering the `NotFound` component, which produces a Not Found page in the browser with a 404 (Not Found) status code.
693
+
:::moniker-end
694
+
695
+
The movie entity's <xref:Microsoft.EntityFrameworkCore.EntityState> is set to <xref:Microsoft.EntityFrameworkCore.EntityState.Modified>, which signifies that the entity is tracked by the context, exists in the database, and that some or all of its property values are modified.
696
+
697
+
:::moniker range=">= aspnetcore-10.0"
698
+
699
+
When a movie isn't found, calling <xref:Microsoft.AspNetCore.Components.NavigationManager.NotFound%2A?displayProperty=nameWithType> renders the `NotFound` component, which produces a Not Found page in the browser with a 404 (Not Found) status code.
649
700
650
701
If there's a concurrency exception and the movie entity no longer exists at the time that changes are saved, the component redirects to the Not Found page, which results in returning a 404 (Not Found) status code. If the movie exists and a concurrency exception is thrown, for example when another user has already modified the entity, the exception is rethrown by the component with the [`throw` statement (C# Language Reference)](/dotnet/csharp/language-reference/statements/exception-handling-statements#the-throw-statement). Additional guidance on handling concurrency with EF Core in Blazor apps is provided by the Blazor documentation.
0 commit comments