我已注册以下过滤器
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new CompressContentAttribute());
filters.Add(new MinifyHtmlAttribute());
filters.Add(new MinifyXmlAttribute());
}
我想忽略特定操作的CompressContentAttribute()
是否有可能在MVC中这样做。
我做了各种研究,每个研究都展示了如何忽略整个过滤器属性
答案 0 :(得分:0)
您需要创建自定义过滤器提供程序。此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());
}