asp.net mvc中的filter——2.IExceptionFilter

本文围绕ASP.NET MVC的IExceptionFilter展开,介绍了HandleErrorAttribute部分源码及customErrors mode配置对异常处理的影响,还提及自定义实现IExceptionFilter、Global.asax中的Application_Error,最后对比两者,指出IExceptionFilter粒度细,Application_Error能捕获应用程序任何异常。

二、IExceptionFilter

2.1 HandleErrorAttribute

  • HandleErrorAttribute部分源码
public class HandleErrorAttribute: FilterAttribute, IExceptionFilter
{
	public string View
	{
		get
		{
			if (string.IsNullOrEmpty(this._view))
			{
				return "Error";
			}
			return this._view;
		}
		set
		{
			this._view = value;
		}
	}
	public override 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() != 500)
		{
			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 = 500;
		filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
	}
}

在这里插入图片描述

  • tips
    • filterContext.HttpContext.IsCustomErrorEnabled会根据web.config中的配置项customErrors mode决定是否继续执行
      • customErrors mode
        • Remote-Only
          • 远程客户端启用自定义错误
          • 本机服务器端显示错误黄页,标识详细错误信息
        • On
          • 远程客户端和本机服务器端都显示自定义错误
        • Off
          • 不显示自定义错误
    • 通过ExceptionContext.Result指定异常处理结果,即相应的ViewResult
      • MVC在执行完IExceptionFilter后还会执行ExceptionContext.Result
    • 错误页面指向Views->Shared->Error.cshtml
  • web.config中的配置项
<system.web>
  <compilation debug="true" targetFramework="4.7.2" />
  <httpRuntime targetFramework="4.7.2" />
  <customErrors mode="On"></customErrors>
</system.web>
  • 错误页面
    在这里插入图片描述
  • 错误黄页
    在这里插入图片描述

2.2 自定义实现IExceptionFilter

public class CustomErrorAttribute: IExceptionFilter
{
	public override void OnException(ExceptionContext filterContext)
	{
		//记日志...
		//跳转错误页面...
	}
}

2.3 Global.asax中的Application_Error

protected void Application_Error()
{
    Exception exception = Server.GetLastError();
    //TODO:记日志或进行业务处理
    Server.ClearError();
}
  • Tips
    • Server
      • HttpApplication的公有属性,类型为HttpServerUtility
      • 提供用于处理 Web 请求的 Helper 方法
    • 最后需要调用Server.ClearError()将错误信息清除,否则会跳转至错误黄页

2.4 IExceptionFilter和Application_Error对比

  • IExceptionFilter
    • 只能捕获在controller及action层面引发的异常
    • 粒度更细
  • Application_Error
    • 能捕获应用程序中的任何异常
    • 粒度更粗
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值