自定义 404 与 500 错误页面,URL 地址不会重定向(一)
对于 404 与 500 错误发生时,我们希望自己定义一个更加人性化的页面。
例子
当访问下面这个地址时:
http://localhost/aaaa/bbb/ccc/ddd/eee/fff/ggg
浏览器的 URL 不变,依然是
http://localhost/aaaa/bbb/ccc/ddd/eee/fff/ggg
但页面显示的是我们自定义的错误页面。
一、在 web.config 里增加以下节点。
<system.webServer>
<httpErrors errorMode="Custom">
<!--跳转 404 页面-->
<remove statusCode="404" subStatusCode='-1' />
<error statusCode="404" path="/error/404" prefixLanguageFilePath="" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
其中:
path="/error/404"
path 的值 “/error/404”是错误页面的 URL 地址,可以自己定义。
当发生 404 错误时就会跳转到该地址。
二、创建 404 错误的控制器。
namespace WebApplication1.Controllers
{
using System.Web.Mvc; public class ErrorController : Controller
{
//
// GET: /Error/
public ActionResult Error404()
{
//这里使用的是“~/Views/Shared/Error.cshtml”页面。
return View("Error");
}
}
}
增加路由规则:
namespace WebApplication1
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing; public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
"error.404",
"error/404",
new { action = "Error404", controller = "Error" }
); //routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
//);
}
}
}
二、创建一个专门用来处理异常的过滤器。
namespace WebApplication1.Filters
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; public class CustomHandleErrorAttribute : HandleErrorAttribute
{
/// <summary>
/// 异常处理。
/// </summary>
/// <param name="filterContext"></param>
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception != null)
{
//返回 500 错误。
filterContext.Result = new ViewResult
{
ViewName = View,
MasterName = Master,
}; filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = ;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
return;
} base.OnException(filterContext);
}
}
}
由于 ASP.NET MVC 已经定义了“HandleErrorAttribute”,只要继承并重写“OnException”方法就可以了。
在 Error.cshtml 页面加上:
Response.Status = "404 Not Found";
否则的话页面状态不正确。
三、将 CustomHandleErrorAttribute 注册成全局过虑器。
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new CustomHandleErrorAttribute());
}
}
完成,现在不管是 404 还是 500 错误,都会使用 “~/Views/Shared/Error.cshtml”页面。
下载地址:
http://files.cnblogs.com/files/cjnmy36723/404%E4%B8%8E500%E9%94%99%E8%AF%AF%E9%A1%B5%E9%9D%A2%E6%BC%94%E7%A4%BA.rar
自定义 404 与 500 错误页面,URL 地址不会重定向(一)的更多相关文章
- 自定义 404 与 500 错误页面,URL 地址不会重定向(二)
上一篇是使用了全局过虑器来实现,还可以使用 HttpApplication 来处理. 参考文章: http://www.cnblogs.com/dudu/p/aspnet_custom_error.h ...
- Nginx 自定义404、500错误页面跳转
自定义Nginx错误界面跳转 1.开启Nginx.conf配置文件下的自定义接口参数. http { fastcgi_intercept_errors on; } 2.在Server区域添加自定义的错 ...
- 【转载】ASP.NET自定义404和500错误页面
在ASP.NET网站项目实际上线运行的过程中,有时候在运行环境下会出现400错误或者500错误,这些错误默认的页面都不友好,比较简单单调,其实我们可以自行设置这些错误所对应的页面,让这些错误跳转到我们 ...
- SpringMVC 指定404、500错误页面
1.在web.xml中追加 <error-page> <error-code>404</error-code> <location>/404</l ...
- asp.net core 自定义404等友好错误页面
Home控制器里: [Route("Home/Error/{statusCode}")] public IActionResult Error(int statusCode) { ...
- ASP.NET MVC 处理404与500错误页面的方法
第一步创建ErrorPageController 第二步添加Oops页面 @{ ViewBag.Title = "Oops"; Layout = "~/Areas/Adm ...
- apache 网页301重定向、自定义400/403/404/500错误页面
首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...
- SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面
SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...
- 404 Not Found错误页面的解决方法和注意事项
最近这段时间一直忙于整理网站的错误页面,期间整理了很多关于404 Not Found错误页面的知识,加之最近也在帮团队新来的人员培训seo优化知识,所以在此借助马海祥博客的平台就拿出来跟大家一起分享一 ...
随机推荐
- E3-1230和E3-1230 V2有多神?
最近追E3-1230,枪E3-1230的人那叫一个多啊,都被捧成神了,我也来说说对E3-1230的看法.同档次的装机方案,我更倾向i5 2320/2500K/3570K. 首 先比较两个U的规格吧.E ...
- C++STL学习笔记_(3)stack
10.2.4stack容器 Stack简介 ² stack是堆栈容器,是一种"先进后出"的容器. ² stack是简单地装饰deque容器而成为另外的一种容器. ² #inc ...
- Junit4单元测试
在Eclipse中使用JUnit4进行单元测试(初级篇) 在Eclipse中使用JUnit4进行单元测试(中级篇) 在Eclipse中使用JUnit4进行单元测试(高级篇)
- 使用Visual Leak Detector检测内存泄漏[转]
1.初识Visual Leak Detector 灵活自由是C/C++语言的一大特色,而这也为C/C++程序员出了一个难题.当程序越来越复杂时,内存的管理也会变得越加复杂,稍有不慎就会出现内存问题 ...
- iOS开发-UIColor转UIIamge方法
只能说太神奇了,完美应用到我的毕业设计 - (UIImage*) createImageWithColor: (UIColor*) color { CGRect rect=CGRectMake(,,s ...
- myeclipse如何导入相应的jar包
方法一: 相应的项目------>右键------->build path-------->Config BuildPath...------->Libraries------ ...
- 记一次Time-Wait导致的问题
去年(2014年)公司决定服务框架改用Finagle(后续文章详细介绍),but 公司业务系统大部分是C#写的,然后 finagle只提供了 scala/java 的Client 于是 只能自己动手丰 ...
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B. Guess the Permutation 水题
B. Guess the Permutation 题目连接: http://www.codeforces.com/contest/618/problem/B Description Bob has a ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...