I've fixed with the following:
Added app.UseStatusCodePagesWithRedirects("/error-page/{0}"); to the Program.cs.
Added the page CustomErrorPage.razor with the following content:
@page "/error-page/{StatusCode:int}"
<div>content</div>
@code {
[Parameter]
public int StatusCode { get; set; }
public bool Is404 => StatusCode == 404;
public string Heading => Is404 ? "Page not found 404" : $"Error {StatusCode}";
}