MVC 6 404 Not Found

前端 未结 6 1473
我在风中等你
我在风中等你 2020-12-06 12:04

Instead of getting a HTTP 404 response, I\'d like to have a generic 404 Not Found page (HTTP 200). I know you can set that up in MVC 5 with



        
6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-06 12:34

    If anyone is looking to create custom 404 pages with HTML, you can do this:

    Startup.cs

    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {        
            app.UseStatusCodePagesWithReExecute("/errors/{0}");
        }
    }
    

    project.json

    "dependencies": {
        "Microsoft.AspNet.Diagnostics": "1.0.0-*",
        // ....
    }
    

    ErrorsController.cs

    public class ErrorsController : Controller
    {
        // ....
    
        [Route("/Error/{statusCode}")]
        public IActionResult ErrorRoute(string statusCode)
        {
            if (statusCode == "500" | statusCode == "404")
            {
                return View(statusCode);
            }
    
            return View("~/Views/Errors/Default.cshtml");
        }
    }
    

    All that's left after this is to create a 404.cshtml, 500.cshtml, and Default.cshtml in your Errors view folder and customize the HTML to your liking.

提交回复
热议问题