English
Français

Blog of Denis VOITURON

for a better .NET world

Blazor - to catch a 404 not found pages.

Posted on 2021-08-14

To capture all routes that do not correspond to any existing page, there are two techniques:

  1. App.razor

This classic method consists in modifying the NotFound tag of the App.razor page. This works very well, as long as you don’t need the content of the erroneous route.

<NotFound>
    <LayoutView Layout="@typeof(MainLayout)">
        <p>Sorry, there's nothing at this address.</p>
    </LayoutView>
</NotFound>
  1. Page CatchAll.razor.

The second way is to create a CatchAll.razor page (or whatever it is called) that intercepts all all routes via @page "/{*Path}". Next, you can log these pages and add a more user-friendly design.

@page "/{*Path}"

<h3>404 - Page not found</h3>
Route: @Path

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

This route catches all requests from the root, but also from all subfolders.

ⓘ With this code, all files (.js, .css, …) will be intercepted. You can catch all routes except “static files” by using the nonfile constraint.

Example: @page "/{*Path:nonfile}"

Languages

EnglishEnglish
FrenchFrançais

Follow me

Recent posts