我想要一个通用的 404 Not Found 页面 (HTTP 200),而不是获得 HTTP 404 响应.我知道您可以使用
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
<customErrors mode="On"> <error statusCode="404" redirect="~/Error/NotFound" /> </customErrors>但我似乎无法弄清楚如何在 MVC 6 中执行此操作.我猜它应该在 UseMvc 路由表或自定义中间件中.
But I can't seem to figure out how to do this in MVC 6. I'm guessing it should be in either the UseMvc routing table, or a custom middleware.
推荐答案Startup.cs:
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:
project.json:
"dependencies": { "Microsoft.AspNet.Diagnostics": "1.0.0-*", // .... }请注意,如果您在 IIS(或 azure)上托管,web.config 仍然有效,并且在某些托管场景中可能需要它.(它应该放在wwwroot文件夹
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
更多推荐
MVC 6 404 未找到
发布评论