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 ...
随机推荐
- vuecli3 运行报错
Microsoft Windows [版本 6.1.7601]版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Users\Administrator\De ...
- Ambari和YARN的Capacity调度器,安装过程
用Spark测试YARN的资源池,测试过程中发现很多时候爆资源不够: 于是添加机器,专门用于跑spark:首先是ssh不通,原来错把71的id_psa.put文件拷贝到64上面:后来ssh通了,amb ...
- (转)用Eclipse 统计代码行数小技巧
今天公司SQA问我目前项目代码行数有多少,我当时就是想,以前好像写过类似的统计工具但是一时又找不到 公司网络又不能下载,所以想想eclipse是不是又类似功能,找了下没有,但突然一想有一个转弯方法:统 ...
- [教程]centos卸载、安装mysql(源码编译安装方式)
-----------1 卸载系统自带的msyql包 rpm -qa|grep mysql rpm -e --nodeps mysql-server-5.1.71-1.el6.x86_64 --强制卸 ...
- 执行 rails server 报错 Could not find a JavaScript runtime
修改第15行 # gem 'therubyracer', platforms: :ruby 去掉注释 执行 bundle install
- Linux_LVM Couldn't find device with uuid
Linux LVM commands result in Couldn't find device with uuid Couldn't find all physical volumes for v ...
- ubuntu上安装Adminer
Apache 安装 $ sudo apt-get install apache2 php 安装 $ sudo apt-get install php7.0 $ sudo apt-get install ...
- 【转】JMeter试用手记
JMeter是一款性能测试工具.个人认为与其说他是一个工具,不如说他是一个框架.因为JMeter的支持范围非常广,目前常见的需要进行性能测试的应用几乎都能应用(如:files, Servlets, P ...
- JVM之运行时数据区
Java虚拟机运行时数据区包括PC寄存器.Java虚拟机栈.Java堆.方法区.本地方法栈.运行时常量池六个部分. 1. PC寄存器 PC寄存器(又叫程序计数器,Program Counter Reg ...
- 使用QuartZ.net来做定时计划任务 ; 值不能为 null。 参数名: assemblyString
1. 当异常的时候, 发现需要的类名, 没有取到, 然后就发生异常了 2. 分析: 业务层调用数据层, 数据层去掉配置的时候, 发现配置文件中根本就没有配置, 这个时候使用反射来取, 肯定是拿不到的, ...