error-handlingasp.net-core-mvcasp.net-core-identityasp.net-core-6.0custom-error-pages

Adding custom 404 error page in ASP.NET Core 6 MVC project integrated with Identity


I'm working on an ASP.NET Core 6 MVC project integrated with the identity for user management.

In the Identity, the AccessDenied page exists by default for 301 error.

I want to add a custom 404 error page like that, but it's not working.

How can I make this pattern work or are there any other solution for adding the custom 404 error page?

I have searched several contents, but not found suitable response.

If someone have the experience for this error page customization, please give me advice.

Thanks in advance.

ASP.NET Core 6 MVC Identity structure


Solution

  • I suggest you could use the UseStatusCodePages middleware to achieve redirect when the 404 error throw.

    More details, you could refer to below codes:

    app.UseStatusCodePages(async context =>
    {
        var response = context.HttpContext.Response;
    
        if (response.StatusCode == 404)
        {
            // Redirect to the custom 404 page
            response.Redirect("/Identity/Account/Error404");
        }
    });