在Global中写一个Application_Error捕捉错误路由并重定向到Not Found页面。这里是全局性抓取错误路由,此处还可以写由错误路由导致访问失败的日志记录。

   protected void Application_Error(object sender, EventArgs e)
{
var httpContext = ((MvcApplication)sender).Context;
var currentController = "";
var currentAction = "";
var currentRouterData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); if (currentRouterData != null)
{
if (currentRouterData.Values["controller"] != null && !string.IsNullOrEmpty(currentRouterData.Values["controller"].ToString()))
{
currentController = currentRouterData.Values["controller"].ToString();
} if (currentRouterData.Values["action"] != null && !string.IsNullOrEmpty(currentRouterData.Values["action"].ToString()))
{
currentAction = currentRouterData.Values["action"].ToString();
}
} var ex = Server.GetLastError();
//record error log here. var controller = new ErrorController();
var routeData = new RouteData();
var action = "Index"; if (ex is HttpException)
{
var httpEx = ex as HttpException; switch (httpEx.GetHttpCode())
{
case :
action = "Not Found";
Response.Redirect("/Error/index");
break;
} httpContext.ClearError();
httpContext.Response.Clear();
httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : ;
httpContext.Response.TrySkipIisCustomErrors = true; routeData.Values["controller"] = "Error";
routeData.Values["action"] = action;
}
}

NotFound页面:

 @{
Layout = null;
} <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="robots" content="noindex,nofollow">
<meta name="viewport" content="width=device-width,maximum-scale=1,user-scalable=no,minimal-ui">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800&amp;subset=latin,latin-ext">
<link rel="stylesheet" type="text/css" href="https://cdn.travel.sygic.com/travel.sygic.com_lp/css/404.css?4aed45ea0fff817941fb48877a968cf6cc920152">
<title>
Not found
</title> </head>
<body>
<div class="stars"></div> <div class="sun-moon">
<div class="sun"></div>
<div class="moon"></div>
</div> <div id="js-hills" class="background hills"></div>
<div id="js-country" class="background country"></div>
<div id="js-foreground" class="background foreground"></div> <div class="error-content">
Sorry, that page never returned<br>
from a Consultant to the <a href="/go/region:377">Marykay</a>.
</div> <a href="https://www.marykay.com.cn/" class="button-home">Go home</a> <div class="code">
<span></span>
<span></span>
<span></span>
</div>
</body>
</html>

在地址栏输入错误的路由-》效果

404页面来源于:

错误的艺术!20个创意的404错误页面设计

http://www.cnblogs.com/lhb25/p/creative-examples-of-404-error-pages.html

最新文章

  1. HashMap与HashTable的区别
  2. salesforce 零基础学习(五十)自定义View或者List以及查看系统原来的View或者List
  3. Service Broker应用(2):不同server间的数据传输,包含集群
  4. ubutu之mysql emma中文乱码问题解决
  5. linux应用于发展(下)
  6. SunSonic 3.0 ORM开源框架的学习
  7. 深入剖析PHP输入流 php://input(与POST/GET的区别)
  8. EasyUi datagrid 表格分页例子
  9. RTP
  10. (转)javascript中 window.location= window.location;是什么意思
  11. Python OpenGL学习(1): 环境配置及错误篇
  12. SqlDataReader中的GetSqlValue()方法
  13. unity中Ray、RaycastHit 、Raycast(小白之路)
  14. [NOI 2011]阿狸的打字机
  15. 使用vue-cli搭建vue项目
  16. 将一幅图像取平均值缩小N倍实现方法
  17. IP白名单
  18. how to get address of member function
  19. 小心踩雷,一次Java内存泄漏排查实战
  20. 第一章javascript词法结构笔记摘要

热门文章

  1. poj 2559 Largest Rectangle in a Histogram 栈
  2. table 中的thead tbody
  3. redis08----集群
  4. CSS中的那点事儿(一)--- CSS中的单位1
  5. SDUT OJ 1598 周游列国
  6. html5--3.2 input元素(2)
  7. 使用cocoaPods加载框架的具体步骤:
  8. OpenCV坐标系与操作像素的四种方法
  9. Watir: Watir-WebDriver对打开的浏览器attach操作
  10. gitbucket