gZip Compression removes pre-existing vary header

We had a requirement to modify Vary Response Header to 'User-Agent' for mobile site SEO for google. Have you faced a situation that your changed Vary header is not getting reflected on the Response Headers on ASP.NET webpage?

We tried to modify the header from Page_Load event of a webpage. but when page is loaded, my HttpWatch/Fiddler is not showing Vary header as 'User-Agent', it is still showing 'Accept-Encoding' instead. Checkout my code:
using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 

public partial class _Default : System.Web.UI.Page 
{ 
   protected void Page_Load(object sender, EventArgs e) 
   { 
      Response.Write("Hello"); 

      // Modify Vary Response Header
      Response.AppendHeader("Vary", "User-Agent"); 
   } 
}
Above code failed so we also tried to append these headers at Page_LoadComplete and context_EndRequest event(last event in page life cycle) assuming that the headers were tampered in between (Between Page_Load and context_EndRequest). See below snap, showing our code not working, means showing Vary as 'Accept-Encoding' instead of 'User-Agent'. So, something strange was happening here.


How we found the cause and the solution

One thing was sure that the headers are overwritten from IIS level, because our modified headers set in context_EndRequest event(last event in page life cycle) were also getting overwritten. After spending few hours, I came to know that dynamic compression module from IIS overwrites the Vary header to 'Accept-Encoding'. Disabling this module solved our problem. Such a nasty bug it is!!

This issue is already addressed by an official patch to IIS. We can download the HotFix from Microsoft - http://support.microsoft.com/kb/2877816

After installing this HotFix, this issue is resolved!!



We can now play with Vary header along with Dynamic Content Compression module!!

Comments