自己测试
测试代码如下:
public class filter1Attribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
}
}
public class filter2Attribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
}
}
public class filter3Attribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
}
}
[filter2]
public class testController : Controller
{
//
// GET: /test/
[filter3]
public ActionResult Index()
{
return View();
}
}
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MvcApplication3.Filters.filter1Attribute());
}
先后顺序
先全局 的fitler1,然后controller 的filter2,然后controller的filter3 默认是3个都执行,没有覆盖之说
就算我把fitler2加上 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
或者 fitler3改为
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class filter3Attribute : Attribute
{
// 摘要:
// 初始化 System.Web.Mvc.AllowAnonymousAttribute 类的新实例。
public filter4Attribute() { }
}
filter1,2,3还是照样执行
就算我去掉全局的filter1,filter2和filter3还是照样执行
就算我吧fiter2,filter3都放action上,还是都执行,如下:
public class testController : Controller
{
[filter2]
[filter3]
public ActionResult Index()
{
return View();
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class filter2Attribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class filter3Attribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
}
}
不知道 AllowMultiple 起什么作用,以后在研究吧
所以看到别人的我很费解,算了,先这么着吧
要想全局里排除某个action什么的,还是在全局的事件中进行判断吧
object[] actionFilter = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoFilterAttribute), false);
object[] controllerFilter = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(NoFilterAttribute), false);
if (controllerFilter.Length == 1 || actionFilter.Length == 1)
{
return;
}