开发者

OutputCache and a custom gzip compression filter

I have this custom filter for compress the output of my pages:

public class EnableCompressionAttribute : ActionFilterAttribute
{
    const CompressionMode compress = CompressionMode.Compress;
    public override void OnActionExecuting(ActionExecutin开发者_运维问答gContext filterContext)
    {
        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 got the code from the book: Pro ASP.NET MVC V2 Framework (Expert's Voice in .NET).

Now I have an action method like this:

[OutputCache(Order=1, Duration=300,VaryByParam="*", VaryByContentEncoding="gzip; deflate")]
[EnableCompression(Order=0)]
public ActionResult About()
{
    return View();
}

How can I ensure that the OutputCache filter is caching the compressed content? Using the "Order" parameter like in this example will be enough?

How can I see what is going on in the cache?

Cheers.

UPDATE: I have tested with Fiddler, apparently it works no matter what order you use on the filters... I get the first response with gzip encoding, and a http.302 in following requests if the client is allowed to cache it, or more http.200 with gzip encoding if just the server is allowed

Probably this is because OutputCache is the last filter by default, and there is no way to change that. May anybody confirm this?


Have a look at this page, http://www.klopfenstein.net/lorenz.aspx/my-take-on-asp-net-output-caching There are some good info there especially Jeff Atwood's advice on compressing cache items

From the page..

Order is important

The ActionFilter above must absolutely be run as last: as I discovered lately, as soon as an action filter changes the action result, the current action invocation is aborted. This also means that all other action filters which did not have a chance to run, will not run, ever. If you plan on adding this caching method to your project, make sure that all filters have the right priority (using the Order priority, that takes a positive integer and orders from lowest to highest).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜