ASP.NET mvc4中的过滤器的使用

2019-05-25 22:31:49王冬梅

继承自ControllerContext的属性

名称

类型

描述

ActionDescripter

ActionDescripter

提供动作方法的细节

Result

ActionResult

用于动作方法的结果,通过非空值可取消请求

Exception

Exception

未处理的异常

ExceptionHandled

Bool

如果另一个过滤器已经把这个异常标记为已处理则返回true

实现自定义异常过滤器

public class RangeExceptionAttribute : FilterAttribute, IExceptionFilter{

    public void OnException(ExceptionContext filterContext){

}

}

使用内建的异常过滤器:

HandleErrorAttribute属性

名称

类型

描述

ExceptionType

Type

由过滤器处理的异常类型

View

String

该过滤器渲染的视图模板名

Master

String

在渲染这个过滤器的视图时使用的布局名称

准备工作:

在web.config文件中启用自定义错误时,HandleErrorAttribute过滤器才会生效,在<system.web>节点中添加一个customErrors属性即可;

<system.web>

 <!--定制错误页aa.html-->

  <customErrors mode="On" defaultRedirect="/Content/aa.html" />

 </system.web>