Controller级别的异常处理过滤器IExceptionFilter
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的更多相关文章
- MVC 身份验证和异常处理过滤器
:在Global中注册为全局过滤器,应用于所有的Controller的Action 参数类均继承自ControllerContext,主要包含属性请求上下文.路由数据.结果 using FilterE ...
- SQL Server错误严重性级别和异常处理
关于SQL Server的错误严重性级别的说明,强烈认真看一下下面的两个链接 脱机帮助 ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.zh-CHS/sqlerrm9/html/ ...
- SpringBoot接口 - 如何优雅的写Controller并统一异常处理?
SpringBoot接口如何对异常进行统一封装,并统一返回呢?以上文的参数校验为例,如何优雅的将参数校验的错误信息统一处理并封装返回呢?@pdai 为什么要优雅的处理异常 如果我们不统一的处理异常,经 ...
- C# Mvc异常处理过滤器
using System; using System.Text; using EMS.Domains.Core; using System.Web.Mvc; using Json.Net; using ...
- Dolphin Scheduler秒级别工作流异常处理
本文章经授权转载 1 组件介绍 Apache Dolphin Scheduler是一个分布式易扩展的可视化DAG工作流任务调度系统.致力于解决数据处理流程中错综复杂的依赖关系,使调度系统在数据处理流程 ...
- ASP.NET MVC5基础-过滤器(Filters)详解
什么是过滤器? 过滤器的类型与作用 定义过滤器 授权过滤器 动作过滤器 结果过滤器 异常处理过滤器 过滤器的使用方法 总结 什么是过滤器? 通过上一篇关于Controller控制器的文章我们知道,MV ...
- SpringBoot系列: Spring支持的异常处理方式
===================================视图函数返回 status code 的方式===================================Spring 有 ...
- 利用过滤器Filter和特性Attribute实现对Web API返回结果的封装和统一异常处理
在我们开发Web API应用的时候,我们可以借鉴ABP框架的过滤器Filter和特性Attribute的应用,实现对Web API返回结果的封装和统一异常处理,本篇随笔介绍利用AuthorizeAtt ...
- 【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常
1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service ...
随机推荐
- 4 字符串 Swift/Objective -C ——《Swift3.0从入门到出家》
4 字符串 Swift and Object-C 字符串由多个字符组成,使用 “” 引起的内容 swift语言中提供了两种字符串:可变字符串和不可变字符串 可变字符串:字符串的内容可以修改,字符串 ...
- JVM介绍(二)
1 JVM简介 JVM是我们Javaer的最基本功底了,刚开始学Java的时候,一般都是从“Hello World”开始的,然后会写个复杂点class,然后再找一些开源框架,比如Spring,Hibe ...
- LinkedList插入排序实现
昨天遇到一个集合排序的问题,要求在list中插入后数据有序,首先考虑使用集合自带的排序方法,但需要把list转成数组,排序后再转回list.后来发现使用插入算法是最省事的,因为既然是在插入里排序,那么 ...
- FPGA中逻辑复制
copy from http://www.cnblogs.com/linjie-swust/archive/2012/03/27/FPGA_verilog.html 在FPGA设计中经常使用到逻辑复制 ...
- sar 命令
sar 命令使用详解 1.使用sar命令查看网络流量(每两秒显示一次,共查看3次): [root@localhost ~]# sar -n DEV 2 3Linux 2.6.32-431.el6.x8 ...
- The java.util.concurrent Synchronizer Framework笔记
这篇笔记是关于 Doug Lea 的 The java.util.concurrent Synchronizer Framework . 原文地址:http://gee.cs.oswego.edu/d ...
- java代码-----逻辑运算符
总结:运算符不熟悉, package com.aa; public class Ss { public static void main(String[] args) { int i=1,j=10; ...
- Tair ldb(leveldb存储引擎)实现介绍
简介 tair 是淘宝自己开发的一个分布式 key/value 存储引擎. tair 分为持久化和非持久化两种使用方式. 非持久化的 tair 可以看成是一个分布式缓存. 持久化的 tair 将数据存 ...
- 微信小程序之页面跳转
方式一: <navigator class="menu_block" url="/pages/address/address"> <text ...
- 【linux】查看进程使用的端口和端口使用情况
netstat -a 查看所有服务端口 netstat -tln 查看当前使用的端口 ps命令查看进程的id: ps aux | grep ftp 或者 pidof Name netstat命 ...