Response compression for specific actions in .NET 8 [duplicate]

I’m upgrading the platform version of my app from .NET Framework 4.8 to .NET 8. I came across an issue when I was trying to add filter which compresses the output. That’s how it looks like in .NET Framework 4.8:

public class CompressAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var encodingsAccepted = filterContext.HttpContext.Request.Headers["Accept-Encoding"];
        if (string.IsNullOrEmpty(encodingsAccepted))
            return;
    
        encodingsAccepted = encodingsAccepted.ToUpperInvariant();
        var response = filterContext.HttpContext.Response;
    
        if (encodingsAccepted.Contains("DEFLATE"))
        {
            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionLevel.Fastest);
    
        }
        else if (encodingsAccepted.Contains("GZIP"))
        {
            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        }
    }
}

And that’s how I modified it:

public class CompressAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var encodingsAccepted = filterContext.HttpContext.Request.Headers.AcceptEncoding.FirstOrDefault()?.ToString();
        if (string.IsNullOrEmpty(encodingsAccepted))
            return;

        encodingsAccepted = encodingsAccepted.ToUpperInvariant();
        var response = filterContext.HttpContext.Response;

        if (encodingsAccepted.Contains("DEFLATE"))
        {
            response.Headers.ContentEncoding = "deflate";
            response.Filter = new DeflateStream(response.Filter, CompressionLevel.Fastest);

        }
        else if (encodingsAccepted.Contains("GZIP"))
        {
            response.Headers.ContentEncoding = "gzip";
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        }
    }
}

Aaaand there’s an error: HttpResponse doesn’t contatin a definition for Filter property. What should I do to achieve the same functionality as before?

  • You need to do this with the help of middleware. You will probably want to use package named Microsoft.AspNetCore.ResponseCompression.

    – 




  • 1

  • So there is no way to implement it in an attribute-like style, only using middleware?

    – 

  • Response compression in ASP.NET was performed through IIS, not custom filters. In any case .NET 8 is .NET Core 8 and ASP.NET Core is a complete rewrite. The pipeline itself is different, the way HttpContext works is different

    – 




  • 1

    The duplicate shows how to do this, using a custom attribute derived from MiddlewareFilterAttribute. That question’s title, Route specific response compression in dotnet core 2 asp .net? asks for the actual problem so it’s a lot easier to find. In fact, I found it by googling for asp.net core response compression per controller once I suspected that’s what you really wanted

    – 

You need to configure middleware for that.
Here is proper example based on package named Microsoft.AspNetCore.ResponseCompression that you will probably want to use.

Leave a Comment