11

I've seen a number of options for adding GZIP/DEFLATE compression to ASP.Net MVC output, but they all seem to apply the compression on-the-fly.. thus do not take advange of caching the compressed content.

Any solutions for enabling caching of the compressed page output? Preferably in the code, so that the MVC code can check if the page has changed, and ship out the precompressed cached content if not.

This question really could apply to regular asp.net as well.

4
  • You mean like caching the compressed response in System.Web.Cache?
    – bzlm
    Oct 1, 2010 at 12:15
  • Perhaps, or any other solutions which accomplish this. Oct 4, 2010 at 20:12
  • None of the answers thus far, while informative in other respects, seem to address compressing content in the cache vs. content served from the cache. Thinking back to a UG presentation on ASP.NET caching, have you considered writing a custom cache provider?
    – J0e3gan
    May 8, 2013 at 15:01
  • Linking this as a possible solution, but I have not tested it: weblogs.asp.net/rashid/archive/2008/03/28/…
    – AaronLS
    May 8, 2013 at 18:52

7 Answers 7

5
[Compress]
[OutputCache(Duration = 600, VaryByParam = "*", VaryByContentEncoding="gzip;deflate")]
public ActionResult Index()
{
    return View();
}
3
  • 2
    Any documentation on this? looks like exactly what Im after. Oct 18, 2010 at 22:37
  • I'd love to see more info on this, I havent been able to make the "Compress" attrib work Aug 29, 2012 at 4:14
  • @BradyMoritz it's possible the Compress attribute is being referenced to something like the following stackoverflow.com/a/3802424/459102 Apr 9, 2019 at 18:45
3

Use caching options using attributes (for MVC), and do not think about compression since IIS/IISExpress automatically compresses your output if you enable it.

the way it works, mvc does not enable caching of individual fragments or parts of output (partial content caching). if you want this, consider using a service like CloudFlare (is there any other like CF?). it automatically caches your output and caches fragments of your output and provides many other performance and security improvements, all without a change in your code.

If this is not an option for you, then you still may use IISpeed (it is a IIS port of Google's mod_pagespeed). It provides some interesting settings like whitespace removal, inline css and js compression, js file merge and many other.

Both CF and IISpeed does not care how your site is built, they work on http/html level, so they both work on MVC, Classic ASP.NET, php or even raw html files.

1
  • 1
    one last one, be careful when you use Vary since it may break caching behavior of some old IE versions. May 7, 2013 at 12:57
1

You can create a attribute like

public class EnableCompressionAttribute : ActionFilterAttribute  
{  
    const CompressionMode Compress = CompressionMode.Compress;  

    public override void OnActionExecuting(ActionExecutingContext filterContext)  
    {  
        HttpRequestBase request = filterContext.HttpContext.Request;  
        HttpResponseBase response = filterContext.HttpContext.Response;  
        string acceptEncoding = request.Headers["Accept-Encoding"];  
        if (acceptEncoding == null)  
            return;  
        else 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");  
        }  
    }  
} 

Add entry in Global.asax.cs

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
        {  
            filters.Add(new EnableCompressionAttribute());  
        }  

Then you can use this attribute as:

    [EnableCompression]
    public ActionResult WithCompression()
    {
        ViewBag.Content = "Compressed";
        return View("Index");
    }

You can download working example from Github: https://github.com/ctesene/TestCompressionActionFilter

6
  • This supports caching the compressed version of the page instead of recompressing each request?
    – AaronLS
    May 8, 2013 at 18:49
  • Yes, the attribute is applied to controller method thus "response" is compressed from server side. I didn't get what do you mean by "re-compressing the request". May 11, 2013 at 23:04
  • Whether or not the compression occurs on every request and is thus inefficient: "Request Received->Retrieve Cached page->Compress cached page", versus just "Request Received->Retrieve Cached page(which is already compressed)".
    – AaronLS
    May 12, 2013 at 1:17
  • I still don't get the actual need or use-case. If the request is cached, the server responds with a 304 status code and does not send the document body to the client, it means no server hit to get cached resource until it's changed on server when request is made. Compression is done on response not on request. what's actual issue you want to get resolved??? May 12, 2013 at 1:32
  • If the page is cached, it should be cached in its compressed form. It is wasteful to recompress the same content on every request.
    – AaronLS
    May 12, 2013 at 1:56
1
+50

This link seems fairly close to what you require. It caches compressed dynamically generated pages. Although the example uses Web forms, It can be adapted to MVC by using an OutputCache attribute

[OutputCache(Duration = 600, VaryByParam = "*", VaryByContentEncoding="gzip;deflate")]
0

You could create a Cache Attribute:

public class CacheAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;

        if (Enabled)
        {
            cache.SetExpires(System.DateTime.Now.AddDays(30));
        }
        else
        {
            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetNoStore();
        }
    }

    public bool Enabled { get; set; }

    public CacheAttribute()
    {
        Enabled = true;
    }
}
2
  • How does this handle compression? Aug 29, 2012 at 4:17
  • Sorry, misunderstood that part of the question. You could check this out weblogs.asp.net/rashid/archive/2008/03/28/…. It is still doing on the fly compression, but its attribute-based giving you more of a pick and choose option that a server-wide solution. Aug 29, 2012 at 14:29
0

See Improving performance with output caching for a full introduction on the subject. The main recommendation is to use the [OutputCache] attribute on the Action to which caching should be applied.

1
  • 2
    Don't see mention of compression though? Oct 18, 2010 at 22:37
0

use namespace

using System.Web.Mvc;

using System.IO.Compression;

create ClassName.cs in you main project

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.ToLowerInvariant();
            var _response = filterContext.HttpContext.Response;
if(_response.Filter == null) return;
            if (_encodingsAccepted.Contains("deflate"))
            {
                _response.AppendHeader("Content-encoding", "deflate");
                _response.Filter = new DeflateStream(_response.Filter, CompressionMode.Compress);
            }
            else if (_encodingsAccepted.Contains("gzip"))
            {
                _response.AppendHeader("Content-encoding", "gzip");
                _response.Filter = new GZipStream(_response.Filter, CompressionMode.Compress);
            }
        }
    }

--- and add in global.asax.cs

GlobalFilters.Filters.Add(new CompressAttribute());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.