惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
GbyAI
GbyAI
博客园 - 叶小钗
Last Week in AI
Last Week in AI
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
云风的 BLOG
云风的 BLOG
C
Check Point Blog
H
Help Net Security
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Y
Y Combinator Blog
U
Unit 42
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
J
Java Code Geeks
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 南阳·源

ThinkPHP运算符与PHP运算符对照表 ThinkPHP常用配置路径 Json数据字符串 反序列化对象时出现错误。遇到意外字符 ICSharpCode.SharpZipLib.dll压缩的zip包,7zip解压时出错 日期转换为中文日期 向大家推荐一款UI对话框框架artDialog Asp.Net MVC3(二)-过滤器定义 Asp.Net MVC3(一)-多Area时,路由设置问题 Json数据序列化对象,及对象序列化为Json格式 序列化/反序化 C# Enum转换 List<T> OrderBy问题 WCF访问安全 跨域访问WCF问题 [转] 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败 Split [转]RegistryKey 操作注册表 js控制ctrl+p sql:过滤字段中是否包含数字
Asp.Net MVC3(三)-MvcApp实现全局异常捕获
南阳·源 · 2013-08-04 · via 博客园 - 南阳·源

定义异常捕获类:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
    public class ExceptionAttribute : FilterAttribute, IExceptionFilter
    {
        public virtual void OnException(ExceptionContext filterContext)
        {
            string message = string.Format("消息类型:{0}<br>消息内容:{1}<br>引发异常的方法:{2}<br>引发异常源:{3}"
                , filterContext.Exception.GetType().Name
                , filterContext.Exception.Message
                 , filterContext.Exception.TargetSite
                 , filterContext.Exception.Source + filterContext.Exception.StackTrace
                 );
            
            //记录日志
            WriteLog.WriteInfo(message);

            //抛出异常信息
            filterContext.Controller.TempData["ExceptionAttributeMessages"] = message;

            //转向
            filterContext.ExceptionHandled = true;
            filterContext.Result = new RedirectResult(Globals.ApplicationDirectory + "/Error/ErrorDetail/");
        }
    }

Mvc全局使用异常捕获类:

Global.asax.cs文件注册全局异常捕获类

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
  filters.Add(new Models.ExceptionAttribute());


  filters.Add(new HandleErrorAttribute());
}

实现其他全局过滤的思想同上.