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”的参数转换失败. 错误 ...
随机推荐
- 微信小程序引入外部js 方法
步骤: 1.首先将外部js放在你指定的文件夹里(这都是废话...) 2.接下来 将该js文件中你要使用的方法给暴露出来 3.在您要使用的js中引入该js 4.使用暴露出来的方法 例子:使用md5加密 ...
- safi 中placeholder不垂直居中
用css hack将line-height 设置为1 例子: input{height: 32px; line-height: 32px; [;line-height: 1px;]};
- UVAlive 6611 Alice's Print Service 二分
Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using h ...
- Vue.js 使用 Swiper.js 在 iOS 11 时出现错误
前言 在H5项目中,需要用到翻页效果,通过 Swiper 来实现,安装 Swiper npm i swiper -S 但是实际使用中,发现低版本 iOS < 11 会出现下面这个错误: Synt ...
- File.Copy的时候Could not find a part of the path
https://developercommunity.visualstudio.com/content/problem/378265/filecopy-did-not-throw-the-correc ...
- YTU 2832: 使用指针访问数组元素--程序填空
2832: 使用指针访问数组元素--程序填空 时间限制: 1 Sec 内存限制: 128 MB 提交: 328 解决: 160 题目描述 输入10个整数值到数组中,使用指针来完成对这10个数组元素 ...
- js实现网页多少秒后自动跳转到指定网址
在网上搜了一下,关于这个技术处理有多种方法,我只记下我在视频里学到的三种: 1.用一个response.sendRedirect("目标页面.jsp\.htm");实现直接跳转: ...
- codeforces 460B Little Dima and Equation 解题报告
题目链接:http://codeforces.com/problemset/problem/460/B 题目意思:给出a, b, c三个数,要你找出所有在 1 ≤ x ≤ 1e9 范围内满足 x = ...
- poj 1274 The Perfect Stall 解题报告
题目链接:http://poj.org/problem?id=1274 题目意思:有 n 头牛,m个stall,每头牛有它钟爱的一些stall,也就是几头牛有可能会钟爱同一个stall,问牛与 sta ...
- 栏目抓取网站日kafka
#!/usr/bin/python3#-*- coding:utf-8 -*-"""create 2018-02-27author zldesc: https://ind ...