ASP.NET MVC4 程序发生异常时,通过拦截Action的异常,重写ActionFilterAttribute 的方法OnActionExecuted实现。

具体实现代码如下:

  1. /// <summary>
  2. /// 拦截Action的异常
  3. /// </summary>
  4. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
  5. public class ExceFilterAttribute : ActionFilterAttribute
  6. {
  7. public override void OnActionExecuted(ActionExecutedContext filterContext)
  8. {
  9. if (filterContext.Exception != null)
  10. {
  11. object[] attrs = filterContext.ActionDescriptor.GetCustomAttributes(typeof(LigerUIExceptionResultAttribute), true);
  12. if (attrs.Length == )//判断是否属于LigerUIResult的Action
  13. {
  14. string msgTmp= "<b>异常消息: </b>{0}</p><b>触发Action: </b>{1}</p><b>异常类型: </b>{2}";
  15. var excResult = new JsonResult();
  16. excResult.Data = AjaxResult.Error(string.Format(msgTmp,
  17. filterContext.Exception.GetBaseException().Message,
  18. filterContext.ActionDescriptor.ActionName,
  19. filterContext.Exception.GetBaseException().GetType().ToString()));
  20. LogHelper.WriteLog("系统错误:" + excResult.Data);
  21. filterContext.Result = excResult;
  22. }
  23. else
  24. {
  25. filterContext.Controller.ViewData["ErrorMessage"] = String.Format(@"<b>异常消息: {0}</br><b>触发Action: </p>{1}</br><b>异常类型: </b>{2}",
  26. filterContext.Exception.GetBaseException().Message,
  27. filterContext.ActionDescriptor.ActionName,
  28. filterContext.Exception.GetBaseException().GetType().ToString());
  29. LogHelper.WriteLog("系统错误:" + filterContext.Controller.ViewData["ErrorMessage"].ToString ());
  30. filterContext.Result = new ViewResult()
  31. {
  32. ViewName = "Error",
  33. ViewData = filterContext.Controller.ViewData,
  34. };
  35.  
  36. }
  37. filterContext.ExceptionHandled = true;
  38. }
  39. }
  40. }

ASP.NET MVC4 异常拦截的更多相关文章

  1. ASP.NET MVC 异常Exception拦截器Fillter

    异常信息的处理在程序中非常重要, 在asp.net mvc中提供异常属性拦截器进行对异常信息的处理,异常拦截器也没有什么的,只是写一个类,继承另一个类(System.Web.Mvc.FilterAtt ...

  2. ASP.NET MVC 异常Exception拦截

    一.前言 由于客户端的环境不一致,有可能会造成我们预计不到的异常错误,所以在项目中,友好的异常信息提示,是非常重要的.在asp.net mvc中实现异常属性拦截也非常简单,只需要继承另一个类(Syst ...

  3. Asp.Netcore使用Filter来实现接口的全局异常拦截,以及前置拦截和后置拦截

    原文链接:https://blog.csdn.net/qq_38762313/article/details/85234594 全局异常拦截器:       解决写每个接口都需要去做容错而添加try{ ...

  4. 最新版CentOS6.5上安装部署ASP.NET MVC4和WebApi

    最新版CentOS6.5上安装部署ASP.NET MVC4和WebApi 使用Jexus5.8.1独立版 http://www.linuxdot.net/ ps:该“独立版”支持64位的CentOS ...

  5. asp.net mvc4 学习笔记一(基本原理)

    做了8年的asp.net webform,用过MVVM但还没用过MVC , 虽然项目不用MVC,但是还是想了解一下,今天第二天学习,以下是学习心得. VS2012默认带有asp.net mvc3和as ...

  6. ASP.NET MVC案例——————拦截器

    摘要      本文将对“MVC公告发布系统”的发布公告功能添加日志功能和异常处理功能,借此来讨论ASP.NET MVC中拦截器的使用方法. 一个小难题      我们继续完善“MVC公告发布系统”, ...

  7. ASP.NET MVC4中调用WEB API的四个方法

    http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...

  8. 使用EF code first和asp.net mvc4遇到的问题总结

    最近使用EF code first和asp.net mvc4做项目,遇到些问题,记录一下. 一.EF code first 生成外键列问题. 一般情况下,都是先写一个int型外键id属性,然后写一个外 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

随机推荐

  1. gedit如何使用代码片段

    gedit的片段插件 1 打开这个插件   2 以HTML为例,我们看到触发器是doctype   3则我在编辑一个HTML文档的时候,先输入doctype,再按Tab键,将会自动提示,我选择即可插入 ...

  2. 用thinkphp操作session

    写了一段代码,对session进行一些常用的操作: <?php namespace Home\Controller; use Think\Controller; class Demo1Contr ...

  3. iOS 7 二维码的生成

    //二维码生成 //UIImageView *theImageView = [[UIImageView alloc]init]; //[self.view addSubview:theImageVie ...

  4. unix 网络编程第八章 UDP

    code 见 https://github.com/juniperdiego/Unix-network-programming-of-mine/tree/master/udpserv01 1 建立so ...

  5. [Done]com.aerospike.client.AerospikeException: Error Code 12: Bin type error

    今天遇到了一个问题:com.aerospike.client.AerospikeException: Error Code 12: Bin type error 异常栈: 网上找了一些资料:https ...

  6. pageEncoding和ContextType区别

    http://blog.csdn.net/kerrywang/article/details/4454895 pageEncoding        在JSP标准的语法中,如果 pageEncodin ...

  7. MySQL 添加外键约束,不检查现有数据

    这可能是MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况. SET FOREIGN_KEY_CHE ...

  8. selenium 实现浏览器 前进和后退

    #coding=utf-8from selenium import webdriver driver = webdriver.Chrome()driver.get("https://www. ...

  9. MySQL数据约束和关联查询

    1 默认值deafult:在建表的时候字段后使用 default ,默认值字段允许为null. 2 非空 not null:在建表的时候字段后使用  not null. 非空字段必须赋值,并且不能是n ...

  10. mysql 字面值

    mysql 数据库中实现了许多的数据类型.通常我们用的最多的是在建表的时候指定列的数据类型 如:brithday date default '2000-01-01' 那么我们如何给字面值(直接量)指定 ...