Keep the optional content compressed within separate folder with its own web.config that compulsorily adds content encoding related attributes to header. For the compulsory content, rename the files to use asp.net runtime and place --> replacing ContentType with intended MIME type, nested within some tag as placing it outermost or uncommented as default irritates during server side xsl transformations. Now implement the following C# logic inside global.asax
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
string acceptEncoding = app.Request.Headers["Accept-Encoding"];
Stream prevUncompressedStream = app.Response.Filter;
if (acceptEncoding == null || acceptEncoding.Length == 0)
return;
acceptEncoding = acceptEncoding.ToLower();
if (acceptEncoding.Contains("gzip"))
{
// gzip
app.Response.Filter = new GZipStream(prevUncompressedStream,
CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "gzip");
} /*else if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
{
// defalte
app.Response.Filter = new DeflateStream(prevUncompressedStream,
CompressionMode.Compress);
app.Response.AppendHeader("Content-Encoding", "deflate");
}*/ // Only cache for X seconds.
Response.Cache.SetExpires(DateTime.Now.AddMonths(9));
Response.Cache.SetMaxAge(new TimeSpan(270, 0, 0,0,0));
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetValidUntilExpires(true);
// Sliding expiration means we reset the X seconds after each request.
// SetETag is required to work around one aspect of sliding expiration.
Response.Cache.SetSlidingExpiration(true);
Response.Cache.SetETagFromFileDependencies();
}