而不是获得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文件夹中