Asp.Net MVC中捕捉错误路由并设置默认Not Found页面。
在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&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
Asp.Net MVC中捕捉错误路由并设置默认Not Found页面。的更多相关文章
- ASP.NET MVC中的错误处理
ASP.NET MVC中的错误的错误处理跨越了两个主要领域:程序异常和路由异常的处理.前者是关于在控制器和视图中捕获错误的;而后者更多是有关重定向和HTTP错误的. 1.在WebConfig中把过滤器 ...
- ASP.NET MVC中的错误-友好的处理方法
转自:http://blog.csdn.net/lizhao1226/article/details/6367400 “/”应用程序中的服务器错误. 无法找到资源. 说明: HTTP 404.您正在查 ...
- 转:在ASP.NET MVC中通过URL路由实现对多语言的支持
对于一个需要支持多语言的Web应用,一个很常见的使用方式就是通过请求地址来控制界面呈现所基于的语言文化,比如我们在表示请求地址的URL中将上语言文化代码(比如en或者en-US)来指导服务器应该采用怎 ...
- Asp.Net MVC中记录错误日志保存到本地txt文件
为了方便查询系统出错弄个错误日志出来对于维护运维来说是很有必要的. 1.在Asp.Net MVC项目中的App_Start添加一个用于处理异常类的文件ErrorLog让他继承HandleErrorAt ...
- 在ASP.NET MVC中使用jQuery的Load方法加载静态页面的一个注意点
使用使用jQuery的Load方法可以加载静态页面,本篇就在ASP.NET MVC下实现. Model先行: public class Article { public int Id { get; s ...
- asp.net mvc 中使用 iframe 加载相应的静态html页面进行显示
<iframe src='<%=ResolveUrl("~/Content/HTML_file/Agreement.html")%>' <%@ Page ...
- ASP.NET MVC中如何在当前页面上弹出另外一个页面
注意:不是链接到另一个页面,而是弹出一个页面,当前的页面和弹出页面都存在于浏览器的同一个标签页中,效果如图: 弹出的窗体置于四大天王页面之上,但是无法继续操作底层的页面,代码如下: function ...
- ASP.NET MVC教程五:ASP.NET MVC中的路由
一.概述 在ASP.NET MVC架构中,控制器在3大核心构件中处于中心地位,通过控制器支配模型和视图,然而从浏览器发出的请求到控制器还需要路由的协助,路由将特定的请求和控制器的动作对应起来. 在AS ...
- asp.net MVC中使用Html.Checkbox提示该字符串未被识别为有效的布尔值错误的解决方法
在asp.net MVC中使用Html.CheckBox提交后出现该字符串未被识别为有效的布尔值错误,或从类型“System.String”到类型“System.Boolean”的参数转换失败. 错误 ...
随机推荐
- Mac开发必备工具(三)—— Fish shell
Fish shell 简介 fish 可以根据输入自动匹配历史命令.它的一大特点是开箱即用,没有zsh那些繁琐的配置.官网:http://www.fishshell.com/. 安装与配置 在终端里使 ...
- hdu-5675 ztr loves math(数学)
题目链接: ztr loves math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- codeforces 667C C. Reberland Linguistics(dp)
题目链接: C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes inpu ...
- 关于布局(Layout)的一切
之前在布局中有很多问题也有很多经验,遗憾都没记下来.现在一点点记下一些东西. 1.外层用LinearLayout的话,常常把orientation设成vertical, android:orienta ...
- cocos2dx常见32种场景切换动画
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init( ...
- Darwin Streaming Server 核心代码分析
基本概念 首先,我针对的代码是Darwin Streaming Server 6.0.3未经任何改动的版本. Darwin Streaming Server从设计模式上看,采用了Reactor的并发服 ...
- 浅谈getAttribute兼容性
最近终于证实tag.setAttribute("style", "color:#000;");在IE7中不起作用.于是百度了一些解决办法. IE的setAttr ...
- Collection View Programming Guide for iOS---(五)---Incorporating Gesture Support
Incorporating Gesture Support 结合手势支持 You can add greater interactivity to your collection views th ...
- 网络爬虫之requests模块的使用+Github自动登入认证
本篇博客将带领大家梳理爬虫中的requests模块,并结合Github的自动登入验证具体讲解requests模块的参数. 一.引入: 我们先来看如下的例子,初步体验下requests模块的使用: ...
- linux中vim常用命令总结