自定义 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优化知识,所以在此借助马海祥博客的平台就拿出来跟大家一起分享一 ...
随机推荐
- socket的一个错误的解释SocketException以及其他几个常见异常
写socket程序有可能会遇见这个问题 其他信息: 由于套接字没有连接并且(当使用一个 sendto 调用发送数据报套接字时)没有提供地址,发送或接收数据的请求没有被接受. 这种情况我的 错误原因 ...
- AJAX的工作原理及其优缺点
1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页开发技术.它使用:使用XHTML ...
- poj1061 青蛙的约会 扩展欧几里德的应用
这个题解得改一下,开始接触数论,这道题目一开始是看了别人的思路做的,后来我又继续以这种方法去做题,发现很困难,学长告诉我先看书,把各种词的定义看懂了,再好好学习,我做了几道朴素的欧几里德,尽管是小学生 ...
- iOS摄像头和相册-UIImagePickerController-浅析
转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101cfd9.html 在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的 ...
- Linux错误代码
#ifndef _I386_ERRNO_H #define _I386_ERRNO_H #define EPERM 1 /* Operation not permitted */ #define EN ...
- CMSIS Example - osTimer osTimerCreate osTimerStart
osTimerId timer; uint32_t cnt=; void timerHandler( void * arg ) { cnt++; osTimerStart( timer, ); } o ...
- Mysql大数据量查询优化
一般MYSQL最基本的分页方式: select * from content order by id desc limit 0, 10 在中小数据量的情况下,这样的SQL足够用了,唯一需要注意的问题就 ...
- 【转】struct和typedef struct
原文:http://www.cnblogs.com/qyaizs/articles/2039101.html 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用type ...
- Codeforces Round #338 (Div. 2) D. Multipliers 数论
D. Multipliers 题目连接: http://codeforces.com/contest/615/problem/D Description Ayrat has number n, rep ...
- 王立平--EditPlus激活码
注冊名:Free User 注冊码:6AC8D-784D8-DDZ95-B8W3A-45TFA