I have converted my Blazor Server-side Application to the new Blazor Web App project. Everything works as expected except for when I navigate to a view that does not exist.
I can reproduce what I am experiencing by creating a default Blazor Web App project in Visual Studio, launching via F5, and navigating to /somepaththatdoesnotexist
(or any URL that does not exist).
What I experience is a blank white page.
What is the recommended configuration to restore the "not found" experience of the previous Blazor Server-side projects?
When creating a new project from the Blazor app template in .NET8, the <NotFound>
tag is not present anymore in the router. And when browsing to an incorrect url, we have a blank page (or the error page of your browser).
To handle the error pages in your blazor app, you can add the line app.UseStatusCodePagesWithRedirects("/StatusCode/{0}");
at the end of your program.cs file, and then create a new razor page :
ErrorPage.razor
@page "/StatusCode/{status}"
<PageTitle>@Status</PageTitle>
<h1>@Status</h1>
@code {
[Parameter]
public int Status { get; set; }
}
You will have the status code in the parameter, and do what you want according to it (404, 500,...)