MVC四大筛选器—ExceptionFilter
该筛选器是在系统出现异常时触发,可以对抛出的异常进行处理。所有的ExceptionFilter筛选器都是实现自IExceptionFilter接口
public interface IExceptionFilter
{
void OnException(ExceptionContext filterContext);
}
实现OnException方法来实现对异常的自定义处理
MVC4中实现了默认的异常处理机制,源码如下
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
if (filterContext.IsChildAction)
{
return;
} // If custom errors are disabled, we need to let the normal ASP.NET exception handler
// execute so that the user can see useful debugging information.
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
{
return;
} Exception exception = filterContext.Exception; // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
// ignore it.
if (new HttpException(null, exception).GetHttpCode() != )
{
return;
} if (!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 = View,
MasterName = Master,
ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = ; // Certain versions of IIS will sometimes use their own error page when
// they detect a server error. Setting this property indicates that we
// want it to try to render ASP.NET MVC's error page instead.
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
Application_Start中将HandleErrorAttribute添加到全局筛选器GlobalFilterCollection中,系统即会对异常进行对应的处理。
我们现在实现一个自定义的异常处理筛选器,在处理完后记录异常信息至日志文件中
public class MyExceptionHandleAttribute : HandleErrorAttribute
{
public MyExceptionHandleAttribute()
: base()
{
} public void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
//记录日志
log.Info(filterContext.Exception);
}
}
在GlobalFilterCollection添加MyExceptionHandleAttribute 即可使用自定义的异常筛选器来处理
MVC四大筛选器—ExceptionFilter的更多相关文章
- MVC四大筛选器—ActionFilter&ResultedFilter
AuthorizeFilter筛选器 在Action的执行中包括两个重要的部分,一个是Action方法本身逻辑代码的执行,第二个就是Action方法的筛选器的执行. MVC4中筛选器都是以AOP(面向 ...
- MVC四大筛选器—AuthorizeFilter
在Action的执行中包括两个重要的部分,一个是Action方法本身逻辑代码的执行,第二个就是Action方法的筛选器的执行. MVC4中筛选器都是以AOP(面向方面编程)的方式来设计的,通过对Act ...
- 在ASP.NET MVC中的四大筛选器(Filter)及验证实现
http://www.cnblogs.com/artech/archive/2012/08/06/action-filter.html http://www.cnblogs.com/ghhlyy/ar ...
- MVC常用筛选器Filter
1.ActionFilterAttribute using System; using System.Collections.Generic; using System.Diagnostics; us ...
- mvc 筛选器
之前公司中,运用ActionFilterAttribute特性实现用户登录信息的验证,没事看了看,留下点东西备忘. 好的,瞅这玩意一眼就大概能猜到这货是干嘛的了吧,没错,action过滤器.其实就是A ...
- Asp.Net mvc筛选器中返回信息中断操作
在mvc中,使用response.end()或Response.Redirect("url"); 是无法阻止请求继续往下执行的.如果在action中,可以我们可以使用return ...
- Asp.Net MVC 页面代码压缩筛选器-自定义删除无效内容
Asp.Net MVC 页面代码压缩筛选器 首先定义以下筛选器,用于代码压缩. /*页面压缩 筛选器*/ public class WhiteSpaceFilter : Stream { privat ...
- 基础教程:ASP.NET Core 2.0 MVC筛选器
问题 如何在ASP.NET Core的MVC请求管道之前和之后运行代码. 解 在一个空的项目中,更新 Startup 类以添加MVC的服务和中间件. publicvoid ConfigureServi ...
- 如何在ASP.NET MVC为Action定义筛选器
在ASP.NET MVC中,经常会用到[Required]等特性,在MVC中,同样可以为Action自定义筛选器,来描述控制器所遵守的规则. 首先,我们在ASP.NET MVC项目中定义一个TestC ...
随机推荐
- ListView的setOnItemClickListener位置错乱问题
如果你对一个ListView同时addHeaderView(listhHeaderView),也就是头部视图,再加setAdapter,当你加上setOnItemClickListener事件后你会发 ...
- 【转载】阿里云Windows服务器重置远程登录密码
在使用阿里云Windows系统的云服务器的时候,有时候忘记了远程连接密码,可在浏览器上登录阿里云官网,进入ECS云服务器管理后台重置远程连接的密码,因为此步操作对于服务器安全来说关系重大,所以有时候在 ...
- 《Office 365开发入门指南教程》正式上线,限时优惠和邀请分享推广
我很高兴地通知大家,<Office 365 开发入门指南教程>已经正式在网易云课堂上线,你可以通过直接访问 https://aka.ms/office365devlesson 这个短地址 ...
- oracle listener.ora文件配置
# listener.ora Network Configuration File: /oracleDB/product//db_1/network/admin/listener.ora # Gene ...
- [android] 短信的广播接收者
比较重要的一个广播事件,短信 界面布局,比如播放视频,默认是横屏全屏的,清单文件中进行设置, 在<activity/>节点设置屏幕朝向属性,android:screenOrientatio ...
- EL表达式的特性
一.EL(Expression Language)表达式语言一.作用:从作用域中取值,再将值显示给客户 二.目的:在JSP中消灭java代码 三.使用:<%@ page isELIgnored= ...
- blfs(systemd版本)学习笔记-编译安装gnome桌面组件及应用
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! blfs中的gnome项目地址:http://www.linuxfromscratch.org/blfs/view/stable ...
- vuejs通过filterBy,orderBy实现搜索筛选,降序排序数据实例
直接贴代码了: 先上输入前的样子: <style> #example{margin:100px auto;width:600px;} .show{margin:10px;} #search ...
- eclipse中maven项目jar包不会自动下载解决办法
Eclipse中maven从远程仓库中下载jar包有时会很慢,有些甚至进度停止不动,这个时候我们可能会终止当前下载,但是终止jar包下载后会出现一个问题,再次打开Eclipse时,你会发现提示你项目中 ...
- Vue 组件之间传值
一.父组件向子组件传递数据 在 Vue 中,可以使用 props 向子组件传递数据. 子组件部分: 这是 header.vue 的 HTML 部分,logo 是在 data 中定义的变量. 如果需要从 ...