Custom 404 Error Page Update

 


In an earlier publication, I discussed the process for developing and configuring a 404 error page within the Blazor framework. Following the release of .NET 8, the methodology for redirecting to the 404 error page has undergone significant changes.

Previously, under versions before .NET 8, configuring the routing for a 404 error page involved appending it within the <NotFound> element of the Router component in the router.razor file. This method is no longer applicable.


New Configuration Approach

The current procedure necessitates the integration of a middleware service into the application pipeline, specifically within the Program.cs file:


            app.UseStatusCodePagesWithRedirects("/CustomErrorPage");


The custom error page must be established as a Razor page, incorporating the @page directive to facilitate its functionality.

Upon encountering a "page not found" error, the application will redirect to the "CustomErrorPage". The creation of this page follows the conventional Razor page development process. In a demonstrative sample page, modifications were made to the layout to enhance its appearance


Important Consideration

It is important to acknowledge the existence of an ErrorPage.razor file, designated for displaying errors stemming from exceptions. It is advisable to avoid naming conflicts with this pre-existing page.


Summary

This brief article has elucidated the updated method for incorporating a custom error page within .NET 8, highlighting a significant modification from previous versions. This change is indicative of Microsoft's efforts to more seamlessly integrate Blazor into the ASP.NET ecosystem, which is advantageous for the development community. 


Comments

Popular posts from this blog

Yes, Blazor Server can scale!

Blazor new and improved Search Box

Blazor - Cool Blur Loading Page