I have implemented one action filter in my MVC project. Now I want to add it globally so that I do not need to write filter attribute just above the action methods. I am using BundleMinifyInlineJsCss nuget package.
I have tried with following code in Global.asax.cs file:
GlobalFilters.Filters.Add(new ReplaceTagsAttribute());
Here is my filter code:
public class ReplaceTagsAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.HttpContext.Response.Filter = new BundleAndMinifyResponseFilter(filterContext.HttpContext.Response.Filter);
}
}
I am getting an error: Filtering is not allowed. How can I declare it Globally?
Thanks.
I think adding a null check will work for you.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With