asp.net mvc HandleErrorAttribute 异常错误处理 无效!
系统未知bug,代码没有深究。
现象:filters.Add(new HandleErrorAttribute()); 使用了全局的异常处理过滤。
HandleErrorAttribute 核心代码:
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction)
{
return;
}
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
}
Exception exception = filterContext.Exception;
if (new HttpException(null, exception).GetHttpCode() != )
{
return;
}
if (!this.ExceptionType.IsInstanceOfType(exception))
{
return;
}
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
filterContext.Result = new ViewResult
{
ViewName = this.View,
MasterName = this.Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = ;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
经过测试,下面这些代码执行完成之后,页面还是显示黄页黄页。而不是系统 默认的Error视图
web.config中配置:customErrors mode="RemoteOnly"
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
有一个项目情况一定是这样的,其他好几个项目正常。
修复代码如下,使用自定义类继承自HandleErrorAttribute 重写方法OnException
加入代码
base.OnException(filterContext);
if (filterContext.ExceptionHandled)
filterContext.HttpContext.ClearError();
关键代码:filterContext.HttpContext.ClearError().
具体原因不明,还有待查证,那位大哥有碰到过??? 猜测可能和httpcontext最后执行的逻辑判断有问题,比如config的配置,运行时参数的不一致等
回家查看asp.net的源代码去,找找ExceptionHandled预计能找到点东西
asp.net mvc HandleErrorAttribute 异常错误处理 无效!的更多相关文章
- [Asp.net MVC]HandleErrorAttribute异常过滤器
摘要 在asp.net mvc中除了使用try...catch/finally来处理异常外,它提供了一种通过在Controller或者Action上添加特性的方式来处理异常. HandleErrorA ...
- ASP.NET MVC中的错误处理
ASP.NET MVC中的错误的错误处理跨越了两个主要领域:程序异常和路由异常的处理.前者是关于在控制器和视图中捕获错误的;而后者更多是有关重定向和HTTP错误的. 1.在WebConfig中把过滤器 ...
- ASP.NET MVC下自定义错误页和展示错误页的几种方式
在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...
- Asp.Net MVC中记录错误日志保存到本地txt文件
为了方便查询系统出错弄个错误日志出来对于维护运维来说是很有必要的. 1.在Asp.Net MVC项目中的App_Start添加一个用于处理异常类的文件ErrorLog让他继承HandleErrorAt ...
- 关于linux asp.net MVC网站中 httpHandlers配置无效的处理方法
近期有Jexus用户反映,在Linux ASP.NET MVC网站的Web.config中添加 httpHandlers 配置用于处理自定义类型,但是在运行中并没有产生预期的效果,服务器返回了404( ...
- 整理ASP.NET MVC 5各种错误请求[401,403,404,500]的拦截及自定义页面处理实例
http://2sharings.com/2015/asp-net-mvc-5-custom-404-500-error-hanlde https://blog.csdn.net/yhyhyhy/ar ...
- ASP.NET MVC 全局异常
先新建一个过滤器ExceptionHandleErrorAttribute.cs 内容如下: using System; using System.Net; using System.Web; usi ...
- WinDBg定位asp.net mvc项目异常崩溃源码位置
项目介绍:asp.net mvc + angular +iis(windows)+windows server 系统莫名崩溃 最近有个系统默认奇妙崩溃50x,服务整体变成无响应,当运维告知我只有重启应 ...
- ASP.NET MVC显示异常信息
开发ASP.NET多了,它的异常信息显示也习惯了.但在ASP.NET MVC中,却是另外一番情形. 以前只习惯使用IE浏览器,现在开发ASP.NET MVC程序,为了捕获到异常信息,Firefox的f ...
随机推荐
- K 均值算法(K-means)
K-means算法是最简单的一种聚类算法.算法的目的是使各个样本与所在类均值的误差平方和达到最小(这也是评价K-means算法最后聚类效果的评价标准) K-means聚类算法的一般步骤: 1. 初始化 ...
- Linux安全基础:awk命令的使用
awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...
- SharePoint 2013 入门教程之入门手册
当我们搭建完环境,创建应用程序和网站集后,就已经正式开启了我们的SharePoint之旅了,进入网站以后,开始基本的使用.设置,了解SharePoint相关特性,下面,来简单了解下SharePoint ...
- StringBuilder用法
//来自MSDN,不太懂英文可以使用百度翻译. //using System; //using System.Text; //public sealed class App //{ // static ...
- C#中Split用法
1.用字符串分隔: using System.Text.RegularExpressions; string str="aaajsbbbjsccc"; string[] sArr ...
- 服务器重启后SQL Server Agent由于"The EventLog service has not been started" 启动失败
案例环境: 操作系统 : Microsoft Windows Server 2003 Standard Edtion SP2 数据库版本 : SQL Server 2005 Standard Ed ...
- [MySQL Reference Manual] 8 优化
8.优化 8.优化 8.1 优化概述 8.2 优化SQL语句 8.2.1 优化SELECT语句 8.2.1.1 SELECT语句的速度 8.2.1.2 WHERE子句优化 8.2.1.3 Range优 ...
- Java:Remote Debug
Java Remote Debug -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,add ...
- 从零自学Hadoop(16):Hive数据导入导出,集群数据迁移上
阅读目录 序 导入文件到Hive 将其他表的查询结果导入表 动态分区插入 将SQL语句的值插入到表中 模拟数据文件下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并 ...
- SQL Server 2008 R2——使用计算列为表创建自定义的自增列
=================================版权声明================================= 版权声明:原创文章 谢绝转载 请通过右侧公告中的“联系邮 ...