如何在Global.asax.cs文件中全局声明Filter属性
本文关键字:全局 声明 Filter 属性 文件 cs Global asax | 更新日期: 2023-09-27 18:11:45
我在MVC项目中实现了一个动作过滤器。现在我想全局地添加它,这样我就不需要在操作方法上面编写过滤器属性。我使用的是bundleminifinlinejss_nuget包。
我在Global.asax.cs文件中尝试了以下代码:
GlobalFilters.Filters.Add(new ReplaceTagsAttribute());
下面是我的过滤器代码:
public class ReplaceTagsAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Filter = new BundleAndMinifyResponseFilter(filterContext.HttpContext.Response.Filter);
}
}
我得到一个错误:过滤是不允许的。我如何全局声明它?
谢谢。
我想加一个空支票会很适合你。
public class ReplaceTagsAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var response = filterContext.HttpContext.Response;
if (response.Filter == null) return; // <-----
response.Filter = new BundleAndMinifyResponseFilter(response.Filter);
}
}