asp.net-core – 返回404页面,不重定向
发布时间:2020-12-16 07:29:50 所属栏目:asp.Net 来源:网络整理
导读:我试图让我的404正常工作,但无法弄清楚如何使它正确. 最初我在我的Statup Configure方法中设置了以下内容: app.UseMvc(routes ={ // routing here});app.Use((context,next) ={ context.Response.StatusCode = 404; return next();});app.UseStatusCodePage
我试图让我的404正常工作,但无法弄清楚如何使它正确.
最初我在我的Statup Configure方法中设置了以下内容: app.UseMvc(routes => { // routing here }); app.Use((context,next) => { context.Response.StatusCode = 404; return next(); }); app.UseStatusCodePagesWithRedirects("/error/{0}"); 哪个重定向到我显示错误的页面.但是,状态代码是302> 200.我设置/ error / {code}动作以返回相关的状态代码,所以现在我有302> 404(由于302重定向)使得它看起来好像/ error / 404页面不存在(它做了). 我想要做的是返回没有重定向的错误页面,因此尝试请求/ doesntexist将返回404并显示错误页面. 我尝试的另一件事是使用app.UseStatusCodePagesWithReExecute(“/ error / {0}”);它确实返回404而不更改URL,但只显示空白页而不是我的错误页 解决方法
在我的应用程序中,我这样做:
// custom 404 and error page - this preserves the status code (ie 404) app.UseStatusCodePagesWithReExecute("/Home/Error/{0}"); 我的HomeController有这个动作方法 public IActionResult Error(int statusCode) { if (statusCode == 404) { var statusFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>(); if (statusFeature != null) { log.LogWarning("handled 404 for url: {OriginalPath}",statusFeature.OriginalPath); } } return View(statusCode); } 我的观点是这样的: @model int @{ switch (Model) { case 400: ViewData["Icon"] = "fa fa-ban text-danger"; ViewData["Title"] = "Bad Request"; ViewData["Description"] = "Your browser sent a request that this server could not understand."; break; case 401: ViewData["Icon"] = "fa fa-ban text-danger"; ViewData["Title"] = "Unauthorized"; ViewData["Description"] = "Sorry,but the page requires authentication."; break; case 403: ViewData["Icon"] = "fa fa-exclamation-circle text-danger"; ViewData["Title"] = "Forbidden"; ViewData["Description"] = "Sorry,but you don't have permission to access this page."; break; case 404: ViewData["Icon"] = "fa fa-exclamation-circle text-danger"; ViewData["Title"] = "Page Not Found"; ViewData["Description"] = "Sorry,but the page you were looking for can't be found."; break; case 500: default: ViewData["Icon"] = "fa fa-exclamation-circle text-danger"; ViewData["Title"] = "Unexpected Error"; ViewData["Description"] = "Well,this is embarrassing. An error occurred while processing your request. Rest assured,this problem has been logged and hamsters have been released to fix the problem."; break; } } <div class="jumbotron text-center"> <header> <h1><span aria-hidden="true" class="@ViewData["Icon"]"></span> @ViewData["Title"]</h1> </header> <p>@ViewData["Description"]</p> <a class="btn btn-primary btn-lg" href="@Url.RouteUrl("/")"><span aria-hidden="true" class="fa fa-home"></span> Site Home</a> </div> 正如@khellang所提到的,中间件的顺序很重要,这应该在app.UseMvc之前 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET vNext Core CLR缺少type.IsPrimitive
- asp.net-mvc – 发布到生产服务器时的.NET MVC错误500
- asp.net 导出excel 中文乱码解决方法
- ASP.Net自定义会话状态管理
- asp.net-mvc-3 – ASP.net MVC路由与可选的第一个参数
- asp.net – 自动化Web应用前端性能测试的替代方案
- 当空的asp.net时,ListView LayoutTemplate不显示
- asp.net – 如何在Button Click事件上调用此Jquery函数?
- asp.net-mvc – AntiXss保护Html模型属性
- ASP.Net中父/子UI设计的最佳实践?
推荐文章
站长推荐
- asp.net ajax实现无刷新验证码
- asp.net-mvc – 为什么log4net不记录nhibernate信
- asp.net-mvc – 使用Fluent验证进行不显眼的自定
- asp.net – ASP .NET ProcessModel配置
- .Net Core MemoryCache PostEvictionCallback无法
- asp.net-mvc – 每个请求DbContext发生随机错误
- 单元测试 – 如何在ASP MVC 5(Microsoft.AspNet.
- asp.net-mvc-3 – 如何使用ASP.Net MVC3隐藏webg
- asp.net – IIS 7 URL重写规则未应用
- asp.net中.aspx页面中各种符号的含义
热点阅读