So, I have a custom attribute called CompressAttribute which is set up as a global filter in global.asax. It uses reflection to examine the return type of the current action
You can also solve this by attaching to OnResultExecuting instead of OnActionExecuting. This gives a few advantages
OnResultExecuting won't execute in exceptional cases (MVC will invoke OnException but not OnResultExecuting)Something like this:
public sealed class MyAttribute : ActionFilterAttribute
{
///
/// Called by MVC just before the result (typically a view) is executing.
///
///
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
var result = filterContext.Result;
if (result is ViewResultBase)
{
var response = filterContext.HttpContext.Response;
// Check your request parameters and attach filter.
}
}