1,系统自带的HandleErrorAttribute类继承自IExceptionFilter,是MVC的默认实现。

同时设置web.config

<system.web>
<customErrors mode="On"/>

</system.web>

//只需要简单的将改特性放到controller类头上,告诉MVC如果该Controller中的Action方法出现异常,都交由HandleError特性处理
[HandleError(ExceptionType = typeof(System.Data.DataException), View = "Error")]
public class HomeController : Controller{
/* Controller Actions with HandleError applied to them */
}

HandleErrorAttribute类中OnException方法的源代码如下:

// System.Web.Mvc.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;
}

2,自定义方式

方式一:

可以重写自定义过滤器,重写HandleErrorAttribute类中的OnException方法

同样设置web.config

<system.web>
<customErrors mode="On"/>
</system.web>

public class MyHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
//自定义
}
}

然后,把自定义过滤器“MyHandleError”放到Controller的头上即可,

[MyHandleError(ExceptionType = typeof(System.Data.DataException), View = "Error")]
public class HomeController : Controller{
/* Controller Actions with HandleError applied to them */
}

方式二:

System.Web.Mvc.Controller类,也继承自IExceptionFilter,但是没有实现OnException方法,因此可以在Controller下重写(实现)OnException方法

同样设置web.config

<system.web>
<customErrors mode="On"/>
</system.web>

public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext); // 当自定义显示错误 mode = On,显示友好错误页面
if (filterContext.HttpContext.IsCustomErrorEnabled)
{
filterContext.ExceptionHandled = true;
this.View("Error").ExecuteResult(this.ControllerContext);
}
}
}

Controller级别的异常处理过滤器IExceptionFilter的更多相关文章

  1. MVC 身份验证和异常处理过滤器

    :在Global中注册为全局过滤器,应用于所有的Controller的Action 参数类均继承自ControllerContext,主要包含属性请求上下文.路由数据.结果 using FilterE ...

  2. SQL Server错误严重性级别和异常处理

    关于SQL Server的错误严重性级别的说明,强烈认真看一下下面的两个链接 脱机帮助 ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.zh-CHS/sqlerrm9/html/ ...

  3. SpringBoot接口 - 如何优雅的写Controller并统一异常处理?

    SpringBoot接口如何对异常进行统一封装,并统一返回呢?以上文的参数校验为例,如何优雅的将参数校验的错误信息统一处理并封装返回呢?@pdai 为什么要优雅的处理异常 如果我们不统一的处理异常,经 ...

  4. C# Mvc异常处理过滤器

    using System; using System.Text; using EMS.Domains.Core; using System.Web.Mvc; using Json.Net; using ...

  5. Dolphin Scheduler秒级别工作流异常处理

    本文章经授权转载 1 组件介绍 Apache Dolphin Scheduler是一个分布式易扩展的可视化DAG工作流任务调度系统.致力于解决数据处理流程中错综复杂的依赖关系,使调度系统在数据处理流程 ...

  6. ASP.NET MVC5基础-过滤器(Filters)详解

    什么是过滤器? 过滤器的类型与作用 定义过滤器 授权过滤器 动作过滤器 结果过滤器 异常处理过滤器 过滤器的使用方法 总结 什么是过滤器? 通过上一篇关于Controller控制器的文章我们知道,MV ...

  7. SpringBoot系列: Spring支持的异常处理方式

    ===================================视图函数返回 status code 的方式===================================Spring 有 ...

  8. 利用过滤器Filter和特性Attribute实现对Web API返回结果的封装和统一异常处理

    在我们开发Web API应用的时候,我们可以借鉴ABP框架的过滤器Filter和特性Attribute的应用,实现对Web API返回结果的封装和统一异常处理,本篇随笔介绍利用AuthorizeAtt ...

  9. 【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常

    1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service ...

随机推荐

  1. Redis事件库源码分析

    由于老大在新项目中使用redis的事件库代替了libevent,我也趁着机会读了一遍redis的事件库代码,第一次读到“优美,让人愉快”的代码,加之用xmind制作的类图非常帅,所以留文纪念. Red ...

  2. bzoj 3295 (洛谷3157、3193) [Cqoi2011]动态逆序对——树套树 / CDQ分治

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3295 题目--洛谷3157:https://www.luogu.org/problemnew ...

  3. eclipse怎么在项目里面批量替换单词

    先选中你要替换的东东,然后再菜单栏中找到Search→Text→Project(先创建要搜索的project),这样就会在整个项目中查找单词.然后在Search的Console中,单击项目,右键选择R ...

  4. 1.使用Fiddler进行接口测试

    1.Fiddler既可以用来抓包数据,亦可以进行接口测试.(可参考我的另一篇博客:https://www.cnblogs.com/android-it/p/9523548.html 进行接口的编写) ...

  5. xshell 使用密钥登录

    http://blog.csdn.net/suquan629/article/details/44783377

  6. 微信小程序之巧妙的封装

    巧妙的封装 暴露一个访问地址xapp.config.js module.exports = { api_host: `https://a.squmo.com/yizu` } 继续引入,加暴露api.c ...

  7. 在rac集群上开启OEM

    由于安装rac的时候没有开启oem,这里开启oem,方便管理 [oracle@rac01 ~]$ emca -config dbcontrol db -repos create -cluster ST ...

  8. Linux Restricted Shell Bypass

    Author: @n4ckhcker & @h4d3sw0rm Introduction Hello, so first of all let’s explain what is a rest ...

  9. C Primer Plus学习笔记(十)- 字符串和字符串函数

    getchar() 和 putchar() getchar() 函数不带任何参数,它从输入队列中返回下一个字符 下面的语句读取下一个字符输入,并把该字符的值赋给变量 ch ch =getchar(); ...

  10. md5加密小程序

    #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import hashlib m = hashlib.md5() m.updat ...