I want to use a custom 404 page and I want to return a 404 status code to the client. One simple way tot accomplish this would be to use
app.UseStatusCodePagesWithReExecute("/404");
So, start with the standard template for a Blazor Web App for .NET 8.
Interactive server-side rendering.
create a razor component, called 404.razor, such as:
@page "/404"
<h3>_404</h3>
In program.cs, just before app.Run(); add
app.UseStatusCodePagesWithReExecute("/404");
Run the app, go to a non-existing page, and get the following error:
InvalidOperationException: Endpoint /404 (/404) contains anti-forgery metadata, but a middleware was not found that supports anti-forgery.
Configure your application startup by adding app.UseAntiforgery() in the application startup code. If there are calls to app.UseRouting() and app.UseEndpoints(...), the call to app.UseAntiforgery() must go between them. Calls to app.UseAntiforgery() must be placed after calls to app.UseAuthentication() and app.UseAuthorization().
But I already have this in program.cs (from the template)
app.UseAntiforgery();
I tried moving the call to UseStatusCodePagesWithReExecute to other locations in program.cs, but cannot get it to work.
Interestingly, if this call is placed anywhere before the call app.UseStaticFiles() then when going to a non-existent page, the content within the “notfound” element of the router element is shown.
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
<NotFound>
The content was not found
</NotFound>
</Router>
So, here are two questions:
- How can I get this simple ReExecute to work properly?
- When does the content in “NotFound” actually display? (starting with the standard template, going to non-existent page just sends a 404 back to client, it doesn’t use the NotFound element)
2