未找到 MVC 6 404 [英] MVC 6 404 Not Found
问题描述
我想要一个通用的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!