MVC 3 compression filter causing garbled output

前端 未结 4 1705
囚心锁ツ
囚心锁ツ 2020-12-28 08:24

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

4条回答
  •  梦毁少年i
    2020-12-28 09:10

    You can also solve this by attaching to OnResultExecuting instead of OnActionExecuting. This gives a few advantages

    1. You can discover the action result without resorting to reflection.
    2. 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.
            }
        }
    

提交回复
热议问题