سؤال

Is there a way to use ASP.NET Outputcache for page for both:

  1. VaryByContentEncoding - whether the browser support gzip or not
  2. By custom param, in my case country which I get from using HttpContext.Current.Request.UserLanguages; and getting the TwoLetterISORegionName from the culture that on of my function returns.

For some countries I return the same page but with minor modifications, but I still want those to be caches separately in order for visitors from different countries to see different social buttons. Of course the support for gzip or not is important because I render a js and css tags with gzip if the browser does support gzip compression.

Update: I thought about using varybycontrol, and put the country + gzip status (o or 1) both into a string and write them into a hidden field and than tell .net to varybycontrol and pass the ID of that hidden field control. I am not sure about this approach though.

Thanks.

هل كانت مفيدة؟

المحلول

When using VaryByCustom for caching, you have access to the whole Request object and you can get the headers from there and create your own cache key based on your requirement.

public override string GetVaryByCustomString(HttpContext context, string arg)
{
  // Do something with Request.Headers["Accept-Encoding"] (like checking if gzip is preferred...)
  return "custom-encoding-key";
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top