asp.net-mvc – 未找到MVC 6 404

而不是获得HTTP 404响应,我想要有一个通用的404 Not Found页面(HTTP 200).我知道你可以在MVC 5中设置
<customErrors mode="On">
  <error statusCode="404" redirect="~/Error/NotFound" />
</customErrors>

但我似乎无法弄清楚如何在MVC 6中做到这一点.我猜测它应该在UseMvc路由表或自定义中间件中.

解决方法

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-*",// ....
}

请注意,如果您在IIS(或天蓝色)上托管,则web.config仍然可以运行,并且在某些主机场景中可能需要它. (它应该放在wwwroot文件夹中

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...