ActionFilterAttribute过滤器
1.要在视图的models层建一个特性类继承过滤器类
public class MyActionAttribute:ActionFilterAttribute
2.实现过滤器类的4个重写方法分别是
OnActionExecuting //action加载前执行
OnActionExecuted //action加载完成执行
OnResultExecuting//视图加载前完成
OnResultExecuted //试图加载完成执行
3.给实现过滤的action或者Controller加上自定义的特性[MyAction]
4.设置全局过滤器在App_start下的 FilterConfig.cs中设置
filters.Add(new MyActionAttribute() {Name ="全局" });//new MyActionAttribute() {Name ="全局" }是自己写的特性执行后在全局生效
注意特性的生效优先级为action>controller>FilterConfig.cs如果想全部特性都实现就要在自己写的特性上加 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
//AttributeUsage(AttributeTargets.Class | AttributeTargets.Method枚举类和属性上Inherited = true可被继承 AllowMultiple = true所有都实现
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class MyActionAttribute:ActionFilterAttribute