您需要創建一個自定義過濾器提供程序。這個IFilterProvider接口有一個方法GetFilters。
public class SelectiveFilterProvider : IFilterProvider
{
private readonly List<Func<ControllerContext, object>> filterconditions = new List<Func<ControllerContext, object>>();
public void Add(Func<ControllerContext, object> mycondition)
{
filterconditions.Add(mycondition);
}
public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
return from filtercondition in filterconditions
select filtercondition(controllerContext) into ctrlContext
where ctrlContext!= null
select new Filter(ctrlContext, FilterScope.Global);
}
}
在Global.asax.cs中
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
MyFilterProvider provider = new MyFilterProvider();
provider.Add(d => d.RouteData.Values["action"].ToString() != "SkipFilterAction1 " ? new CompressContentAttribute() : null);
FilterProviders.Providers.Add(provider);
filters.Add(new HandleErrorAttribute());
filters.Add(new MinifyHtmlAttribute());
filters.Add(new MinifyXmlAttribute());
}