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统一处理异常的更多相关文章

  1. SpringMVC第六篇【校验、统一处理异常】

    Validation 在我们的Struts2中,我们是继承ActionSupport来实现校验的-它有两种方式来实现校验的功能 手写代码 XML配置 这两种方式也是可以特定处理方法或者整个Action ...

  2. SpringMVC【校验器、统一处理异常、RESTful、拦截器】

    前言 本博文主要讲解的知识点如下: 校验器 统一处理异常 RESTful 拦截器 Validation 在我们的Struts2中,我们是继承ActionSupport来实现校验的...它有两种方式来实 ...

  3. 本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善(转)

    本版本延续MVC中的统一验证机制~续的这篇文章,本篇主要是对验证基类的扩展和改善 namespace Web.Mvc.Extensions { #region 验证基类 /// <summary ...

  4. MVC默认提供了一个异常过滤器 HandleErrorAttribte特性

    这一篇记录MVC默认提供了一个异常过滤器 HandleErrorAttribte,下一篇介绍自定义异常过滤特性. 参考引用:https://www.cnblogs.com/TomXu/archive/ ...

  5. SpringBoot统一处理异常

    在springboot项目,报错有着默认的提示,这篇文章介绍一下如何统一处理异常. 新建项目,pom文件如下: <?xml version="1.0" encoding=&q ...

  6. 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 ...

  7. @ExceptionHandler和@ControllerAdvice统一处理异常

    //@ExceptionHandler和@ControllerAdvice统一处理异常//统一处理异常的controller需要放在和普通controller同级的包下,或者在ComponentSca ...

  8. 只需一步,在Spring Boot中统一Restful API返回值格式与统一处理异常

    ## 统一返回值 在前后端分离大行其道的今天,有一个统一的返回值格式不仅能使我们的接口看起来更漂亮,而且还可以使前端可以统一处理很多东西,避免很多问题的产生. 比较通用的返回值格式如下: ```jav ...

  9. .NET MVC Json()处理大数据异常解决方法

    [1-部分原文]: .NET MVC Json()处理大数据异常解决方法 整个项目采用微软的ASP.NET MVC3进行开发,前端显示采用EasyUI框架,图表的显示用的是Highcharts,主要进 ...

随机推荐

  1. [Effective JavaScript笔记]第3条:当心隐式的强制转换

    js对类型错误出奇的宽容 3+true;  //4 3*””;  //0 3+[]; //3 3+[3]; //33 以上表达式在许多语言早就变红了.而js不但不报错还给你个结果. 极少情况会产生即时 ...

  2. NGUI无限滑动

    http://www.unity蛮牛.com/blog-9383-1391.html 最近由于工作需要,就开始研究NGUI滑动.刚开始参考NGUI自带的循环滑动,利用隐藏和显示,提高GPU的渲染,但是 ...

  3. unity3d进行脚本资源打包加载

    原地址:http://www.cnblogs.com/hisiqi/p/3204752.html 本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public c ...

  4. 在mac上安装nodejs

    文章转载自我的个人博客  www.iwangzheng.com node.js最初是2009年发布的,目标是为聊实现事件驱动和非阻塞I/O的web服务器,应用的场景非常的广泛,有web服务器.实时应用 ...

  5. Lucas的数论题解

    Lucas的数论 reference 题目在这里> < Pre 数论分块 默认向下取整时. 形如\(\sum\limits_{i=1}^n f\left( \frac{n}{i}\righ ...

  6. 《ASP.NET1200例》<asp:DataList>分页显示图片

    aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...

  7. Lowest Common Ancestor

    Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes ...

  8. MQTT——安装、测试

    MQTT学习笔记——MQTT协议体验 Mosquitto安装和使用         http://blog.csdn.net/xukai871105/article/details/39252653 ...

  9. html css js

    html 回顾 字体:font 属性: color: 颜色 size: 字号 表格:table 标签: tr:表格中的行 td: 单元行中的单元格 th:通常使用在table中的第一行, 成为表头, ...

  10. bootstrap的select2校验及不影响原来的格式

    <style> .has-error .select2-choice, .has-error .select2-choices, .has-error.simple .select2-ch ...