Mvc 异常过滤器

MVC自带一个异常过滤器即 HandleErrorAttribute

1.首先要进行配置web.config

defaultRedirect表示需要跳转的错误页面,mode需设置为 on或者 RemoteOnly.

注:HandleError只处理服务器500错误,404、400等这些错误不进行处理,如果需要分别给其定义错误页面,则在<customErrors><customErrors/>节点里增加<error  statusCode="404" redirect="InternalError.htm"/>

2.自定义跳转错误页

[HandleError(View="TestError.htm")]置于控制器action上方即作为特性使用,则该方法抛出异常时进入定义的错误页面

3.定义全局异常过滤器

  在Global.asax下有个RegisterGlobalFilters 方法,在方法内添加 filters.Add(new HandleErrorAttribute());

 自定义异常过滤器  继承IExceptionFilter类进行自定义

原文地址:https://www.cnblogs.com/97310ZT/p/8417775.html