Click here to Skip to main content
15,894,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a compressed filter: CompressAttribute, set up a global filter, but there was a problem, whenever an exception is thrown, also will be compressed, so the page will appear garbled.
This is a regular file compression feature code
C#
public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        object[] actionFilter = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NoCompress), false);
        object[] controllerFilter = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(NoCompress), false);
        if (controllerFilter.Length == 1 || actionFilter.Length == 1)
        {
            return;
        }

        if (filterContext.HttpContext.Request.IsAjaxRequest())
        {
            return;
        }


        HttpRequestBase request = filterContext.HttpContext.Request;
        HttpResponseBase response = filterContext.HttpContext.Response; string acceptEncoding = request.Headers["Accept-Encoding"];
        if (acceptEncoding == null)
            return;

        if (acceptEncoding.ToLower().Contains("gzip"))
        {
            response.Filter = new GZipStream(response.Filter, compress);
            response.AppendHeader("Content-Encoding", "gzip");
        }
        else if (acceptEncoding.ToLower().Contains("deflate"))
        {
            response.Filter = new DeflateStream(response.Filter, compress);
            response.AppendHeader("Content-Encoding", "deflate");
        }
    }


I thought, inheritance IException interface implementations, whenever abnormal, I on GZIP compression decompression, but no effect, or still appear messy code problem. The following is my decompression code.

C#
public void OnException(ExceptionContext filterContext)
    {
        string acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
        if (String.IsNullOrEmpty(acceptEncoding)) return;
        var response = filterContext.HttpContext.Response;
        acceptEncoding = acceptEncoding.ToUpperInvariant();
        var obj = new GZipStream(response.Filter, decompress);
        if (acceptEncoding.Contains("GZIP"))
        {
         //   response.AppendHeader("Content-Encoding", "gzip");
            response.Filter = obj;
        }
        else if (acceptEncoding.Contains("DEFLATE"))
        {
            //   response.AppendHeader("Content-Encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, decompress);
        }
    }


I don't know where I was wrong, ask everybody to help me,thank you!
Posted

1 solution

Hope everybody to help me, this bothers me for a long time
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900