0

I am mainly using Interactive render mode: WebAssembly and Interactivity location: Global, but the issue exists with each template setting. Can someone give me an indication of what I need to do to implement a simple 404 page?

Update: I was told to add app.UseStatusCodePagesWithReExecute( "/StatusCode/{0}" ); to my Program.cs (in this case, the server-side one), and then add a page StatusCode.razor (in this case, on the client) to output the status code:

@page "/StatusCode/{ResponseCode}"

@inherits MainLayout;

<p>@this.ResponseCode</p>

@code {
    [Parameter]
    public string? ResponseCode { get; set; }
}

This seems to work, but then a moment later it gets replaced with a blank page displaying only Not found. I assume this is due to some middleware or other, but I have further idea at the moment.

1

1 Answer 1

1

There is an easy way to do through MapFallback.

server-side Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveWebAssemblyComponents();

var app = builder.Build();

...

app.MapRazorComponents<App>()
    .AddInteractiveWebAssemblyRenderMode()
    .AddAdditionalAssemblies(typeof(BlazorApp1.Client._Imports).Assembly);

app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("404page.html");

app.Run();

client-side wwwroot/404page.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h1>Sorry, we couldn't find the page you're looking for</h1>
</body>
</html>

enter image description here

1
  • Well, that meets the requirements of the question. I guess it also gives me a place to look for more options to report the nature of routing errors or whatever could apply.
    – hamstar
    Commented Mar 6 at 2:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.