MVC 使用HandleErrorAttribute统一处理异常
HandleErrorAttribute继承自FilterAttribute,且实现了IExceptionFilter接口。
属于AOP思想的一种实现,MVC的四大筛选器(权限,动作,结果,异常)中的异常处理。
Points:
1)ExceptionHandled为true,表明异常已经处理,不再被拦截;反之,可能被再次拦截;
2)通过设置ExceptionContext的Result(ActionResult类型)属性,表明结束当前请求。
Usage
1.创建自定义异常处理
public class AppHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
var json = new JsonResponse();
json.success = false;
var ex = filterContext.Exception;
//deal custom exception…
json.message = "系统异常,请联系管理员!";
string msg = string.Format("系统异常:{0}", ex.Message);
LogHelper.Error(msg, ex); //Log4jNet filterContext.ExceptionHandled = true;
var result = new JsonResult();
result.Data = json;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
filterContext.Result = result; #region Sample Segment(样例)
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
JsonResult jsonResult = new JsonResult()
{
Data = ""
}; LogHelper.Error(filterContext.Exception.Message); filterContext.Result = jsonResult;
}
else
{
filterContext.Result = new RedirectResult("Error");
} filterContext.ExceptionHandled = true;
#endregion
}
}
2.注册
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new AppHandleErrorAttribute());
}
}
参考:http://www.cnblogs.com/linjiancun/archive/2010/09/16/1827937.html
3.使用
如果要运用AppHandleError拦截异常,则将Controller继承自BaseController即可。
[AppHandleError]
public abstract class BaseController : Controller
{
//......
}
MVC 使用HandleErrorAttribute统一处理异常的更多相关文章
- SpringMVC第六篇【校验、统一处理异常】
Validation 在我们的Struts2中,我们是继承ActionSupport来实现校验的-它有两种方式来实现校验的功能 手写代码 XML配置 这两种方式也是可以特定处理方法或者整个Action ...
- SpringMVC【校验器、统一处理异常、RESTful、拦截器】
前言 本博文主要讲解的知识点如下: 校验器 统一处理异常 RESTful 拦截器 Validation 在我们的Struts2中,我们是继承ActionSupport来实现校验的...它有两种方式来实 ...
- 本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善(转)
本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善 namespace Web.Mvc.Extensions { #region 验证基类 /// <summary ...
- MVC默认提供了一个异常过滤器 HandleErrorAttribte特性
这一篇记录MVC默认提供了一个异常过滤器 HandleErrorAttribte,下一篇介绍自定义异常过滤特性. 参考引用:https://www.cnblogs.com/TomXu/archive/ ...
- SpringBoot统一处理异常
在springboot项目,报错有着默认的提示,这篇文章介绍一下如何统一处理异常. 新建项目,pom文件如下: <?xml version="1.0" encoding=&q ...
- The prefix "mvc" for element "mvc:annotation-driven" is not bound 异常
https://www.cnblogs.com/maodot/p/7531042.html The prefix "mvc" for element "mvc:annot ...
- @ExceptionHandler和@ControllerAdvice统一处理异常
//@ExceptionHandler和@ControllerAdvice统一处理异常//统一处理异常的controller需要放在和普通controller同级的包下,或者在ComponentSca ...
- 只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常
## 统一返回值 在前后端分离大行其道的今天,有一个统一的返回值格式不仅能使我们的接口看起来更漂亮,而且还可以使前端可以统一处理很多东西,避免很多问题的产生. 比较通用的返回值格式如下: ```jav ...
- .NET MVC Json()处理大数据异常解决方法
[1-部分原文]: .NET MVC Json()处理大数据异常解决方法 整个项目采用微软的ASP.NET MVC3进行开发,前端显示采用EasyUI框架,图表的显示用的是Highcharts,主要进 ...
随机推荐
- smb
smb编辑 SMB(Server Message Block)是协议名,它能被用于Wap连接和客户端与服务器之间的信息沟通.
- linux配置ant
第1步:下载ant ,地址:http://ftp.twaren.net/Unix/Web/apache//ant/binaries/apache-ant-1.9.6-bin.tar.gz apache ...
- xcode6 使用MJRefresh
1. MJRefreshConst.m 里面 会报错: unknown type 'NSString'... 原因: xcode6 取消.pch文件, 所以没有导入 foundation和uikit ...
- 练习英语ing——[POJ1004]Financial Management
[POJ1004]Financial Management 试题描述 Larry graduated this year and finally has a job. He's making a lo ...
- HDOJ 1875
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- [ruby on rails] 跟我学之(1)环境搭建
环境: ubuntu 12.04 (64bit) 代理: 自己最好弄一个代理. 环境配置指令如下: sudo apt-get update sudo apt-get install curl \cur ...
- 六间房 繁星 酷我 来疯 秀吧 新浪秀 直播播放器 Live 1.2
适合用于进行录制的时候 特别说明: 安装 falsh play 19 时 不能正常播放 每个按钮都有提示,不详细说明 下载地址 http://pan.baidu.com/s/1i32ETIt 下载地址 ...
- Java for LeetCode 160 Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- CocoaPods报错及解决方法记录
[!] Oh no, an error occurred. Search for existing GitHub issues similar to yours: https://github.com ...
- Java Hour 34 Weather ( 7 ) struts2 – validate
有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. Hour 34 Form Validation 一般Form 提交都有验证的, ...