MVC 6 404 Not Found

前端 未结 6 1496
我在风中等你
我在风中等你 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:42

    Startup.cs:

    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            // PICK YOUR FLAVOR.
    
            // app.UseErrorPage(ErrorPageOptions.ShowAll);
            app.UseStatusCodePages(); // There is a default response but any of the following can be used to change the behavior.
    
            // app.UseStatusCodePages(context => context.HttpContext.Response.SendAsync("Handler, status code: " + context.HttpContext.Response.StatusCode, "text/plain"));
            // app.UseStatusCodePages("text/plain", "Response, status code: {0}");
            // app.UseStatusCodePagesWithRedirects("~/errors/{0}"); // PathBase relative
            // app.UseStatusCodePagesWithRedirects("/base/errors/{0}"); // Absolute
            // app.UseStatusCodePages(builder => builder.UseWelcomePage());
            // app.UseStatusCodePagesWithReExecute("/errors/{0}");
        }
    } 
    

    project.json:

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

    note that, if you are hosting on IIS (or azure), the web.config does still works and it's big maybe needed in some hosting scenarios. (it should be placed inside wwwroot folder

提交回复
热议问题