Search code examples
c#asp.net.netasp.net-core.net-7.0

The 404 page works with problems in .NET7


i make 404page IN .NET 7 with this code

app.UseStatusCodePagesWithReExecute("/Error404");

My problem is that when I enter the URL example.com/contactus/form/gdhsgdg, which does not exist, the 404 page does not show up. However, when I enter the URL example.com/contactus/formdsjhdshg, which also does not exist, the 404 page shows up.

I want the 404 page to be displayed by entering an address like example.com/contactus/form/gdhsgdg.


Solution

  • Do you want below?

    In controller:

            [Route("Error/404")]
            public IActionResult Error404(int code)
            {
    
                return View("Error404");
            }
    

    Error404 view:

    this  is error 404
    

    Program.cs:

    app.UseStatusCodePagesWithReExecute("/Error/{0}");
    

    Update:

    Try to add a route attribute:

           [Route("/contactus/form/")]
            public IActionResult form()
            {
                return View();
            }
    

    result:

    enter image description here