对于 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 地址不会重定向(一)的更多相关文章

  1. 自定义 404 与 500 错误页面,URL 地址不会重定向(二)

    上一篇是使用了全局过虑器来实现,还可以使用 HttpApplication 来处理. 参考文章: http://www.cnblogs.com/dudu/p/aspnet_custom_error.h ...

  2. Nginx 自定义404、500错误页面跳转

    自定义Nginx错误界面跳转 1.开启Nginx.conf配置文件下的自定义接口参数. http { fastcgi_intercept_errors on; } 2.在Server区域添加自定义的错 ...

  3. 【转载】ASP.NET自定义404和500错误页面

    在ASP.NET网站项目实际上线运行的过程中,有时候在运行环境下会出现400错误或者500错误,这些错误默认的页面都不友好,比较简单单调,其实我们可以自行设置这些错误所对应的页面,让这些错误跳转到我们 ...

  4. SpringMVC 指定404、500错误页面

    1.在web.xml中追加 <error-page> <error-code>404</error-code> <location>/404</l ...

  5. asp.net core 自定义404等友好错误页面

    Home控制器里: [Route("Home/Error/{statusCode}")] public IActionResult Error(int statusCode) { ...

  6. ASP.NET MVC 处理404与500错误页面的方法

    第一步创建ErrorPageController 第二步添加Oops页面 @{ ViewBag.Title = "Oops"; Layout = "~/Areas/Adm ...

  7. apache 网页301重定向、自定义400/403/404/500错误页面

    首先简单介绍一下,.htaccess文件是Apache服务器中的一个配置文件(Nginx服务器没有),它负责相关目录下的网页配置.通过对.htaccess文件进行设置,可以帮我们实现:网页301重定向 ...

  8. SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面

    SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...

  9. 404 Not Found错误页面的解决方法和注意事项

    最近这段时间一直忙于整理网站的错误页面,期间整理了很多关于404 Not Found错误页面的知识,加之最近也在帮团队新来的人员培训seo优化知识,所以在此借助马海祥博客的平台就拿出来跟大家一起分享一 ...

随机推荐

  1. 咏南多层开发框架支持最新的DELPHI 10 SEATTLE

    购买了咏南多层开发框架的老用户如有需要提供免费升级. 中间件

  2. Swift-CALayer十则示例

    作者:Scott Gardner   译者:TurtleFromMars原文:CALayer in iOS with Swift: 10 Examples 如你所知,我们在iOS应用中看到的都是视图( ...

  3. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  4. java获取数据库的所有列名和对应的数据库类型

    /**     * 连接数据库     * @param driver 数据库的驱动类     * @param url 数据库的地址     * @param userName 数据库的用户名   ...

  5. 从scanf的学习接口设计

    对大多数程序员来说scanf可以能是最熟悉,也是陌生的工具.在学习C语言时,大家一定没少用它,但是对它也知道不多.比如说,它有哪些可能的返回值?又比如怎么样才能跳过回车,读一个字符?我们可以一起来研究 ...

  6. ASP.NET基础之HttpModule 、HttpContext、 HttpHandler

    http://www.cnblogs.com/wujy/p/3261141.html http://www.cnblogs.com/wujy/p/3264475.html http://www.cnb ...

  7. JavaScript 各种遍历方式详解,有你不知道的黑科技

    http://segmentfault.com/a/1190000003968126 为了方便例子讲解,现有数组和json对象如下 var demoArr = ['Javascript', 'Gulp ...

  8. Flex圆角矩形

    <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="h ...

  9. DAG成员服务器还原

    DAG成员服务器 exmb02 已损坏: 1.使用 Get-MailboxDatabase cmdlet 为要恢复的服务器上的任何邮箱数据库副本检索所有重播延迟和截断延迟设置:   Get-Mailb ...

  10. GridView实现多表头合并[转]

    1.这里先介绍单纯的GridView多表头合并,先上图: 可以看到,上图就是生成的多表头,具体的后台代码是在Row_Created事件中创建的.先看创建代码: protected void GridV ...