White search icon
News
ASP.NET 404 default page

How to create a default 404 error page in ASP.NET 6

Original YouTube Logo

Article have related YouTube video

To have a default 404 page on your website can ensure that you don’t lose your readers and also improve your websites SEO ranking. To make such error-page in ASP.NET 6 is actually extremely simple and will help you keep your readers on your website.

19-06-2022 |

ASP.NET Core
SEO
Razor Pages

The reason it will keep the readers on your website is because they will still be on your website with a menu to navigate than a browser default 404 page that will just make the user go to another page via the URL. The only thing we need to do to add a 404 page to your ASP.NET website is to add one line of code in your program.cs file and then make a new razor page template for the actual default 404 error page.

Program.cs

  
app.UseStatusCodePagesWithRedirects("/errors/{0}");


Create a folder in your “Pages” folder called “errors”. Inside the “errors” folder you can then add a razor page called 404.cshtml. You can make this file look exactly how you want to. However this is a template I just made.

errors/404.cshtml

  
@page
@model website.Pages.errors._404Model
@{
    ViewData["title"] = "Page does not exists";
}


<div class="container" style="text-align: center;  color: #888;">
    <h1 style="font-size: 150px; color: #888; margin-bottom: 0px;">404</h1>
    <p style="font-size: 50px; ">Page not found</p>
    <p>The page you are looking for doesn't exists or another issue occurred.</p>
    <p>Please navigate back to zetbit.tech</p>
</div>


Like what you see?

Buy me a coffee

New C# Learning Area - Free!

Our website now offers a new section on C# theory, including code examples and related video tutorials.

An unhandled error has occurred. Reload 🗙

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.