Server can not attach a header after the HTTP headers have been sent

2

Good morning,

I have the following code that I use to compress the controllers using GZip .

I log in to the system normally, but if I try to change the company, the following error is displayed:

  

Server can not attach a header after the HTTP headers have been sent

Line error:

Response.AppendHeader("Content-Encoding", "gzip");

Code used for compression:

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace EngemanMVC.MLib
{
    /// <summary>
    /// Attribute that can be added to controller methods to force content
    /// to be GZip encoded if the client supports it
    /// </summary>
    public class CompressContentAttribute : ActionFilterAttribute
    {

        /// <summary>
        /// Override to compress the content that is generated by
        /// an action method.
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            GZipEncodePage();
        }

        /// <summary>
        /// Determines if GZip is supported
        /// </summary>
        /// <returns></returns>
        public static bool IsGZipSupported()
        {
            string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
            if (!string.IsNullOrEmpty(AcceptEncoding) &&
                    (AcceptEncoding.Contains("gzip") || AcceptEncoding.Contains("deflate")))
                return true;
            return false;
        }

        /// <summary>
        /// Sets up the current page or handler to use GZip through a Response.Filter
        /// IMPORTANT:  
        /// You have to call this method before any output is generated!
        /// </summary>
        public static void GZipEncodePage()
        {
            HttpResponse Response = HttpContext.Current.Response;

            if (IsGZipSupported())
            {
                string AcceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];

                if (AcceptEncoding.Contains("gzip"))
                {
                    if (Response.Filter != null)
                    {
                        Response.Filter = new System.IO.Compression.GZipStream(Response.Filter,
                                                    System.IO.Compression.CompressionMode.Compress);
                        Response.Headers.Remove("Content-Encoding");
                        Response.AppendHeader("Content-Encoding", "gzip");
                    }
                }
                else
                {
                    if (Response.Filter != null)
                    {
                        Response.Filter = new System.IO.Compression.DeflateStream(Response.Filter,
                                                System.IO.Compression.CompressionMode.Compress);
                        Response.Headers.Remove("Content-Encoding");
                        Response.AppendHeader("Content-Encoding", "deflate");
                    }
                }


            }

            // Allow proxy servers to cache encoded and unencoded versions separately
            Response.AppendHeader("Vary", "Content-Encoding");
        }
    }
}

Note: If I continue the program it works normally, but I wanted to avoid putting an empty try / catch because it is neither recommendable nor beautiful.

    
asked by anonymous 27.12.2018 / 11:46

0 answers