MVC 6 404 未找到

编程入门 行业动态 更新时间:2024-10-21 23:13:10
本文介绍了MVC 6 404 未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要一个通用的 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 未找到

本文发布于:2023-11-08 13:59:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:未找到   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!