Thursday, 28 September 2017

Website Performance with ASP.NET - Use Cache Headers (HttpModule CacheHeader define)

Setting Header Programatically

You can also set the cache headers programmatically. This can be useful for generated content and allows more fine granular and flexible control of the cache headers. The example below allows clients as well as proxies to cache all responses for one hour. You can of course use different settings depending on any parameter/content/config value etc.

using System;
using System.Web;
using System.Web.UI;
using System.Web.Configuration;
using System.Text;
using System.Security.Cryptography;
using System.IO;
public class PreInitModule : IHttpModule
{
public void Init(HttpApplication context)
    {
     
        context.PreRequestHandlerExecute += HandlePreRequest;
    }
    private void SetDefaultCacheHeader(object sender, EventArgs eventArgs)
    {
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
        HttpContext.Current.Response.Cache.SetMaxAge(TimeSpan.FromSeconds(3600));
        HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(3600));
    }
}


Web.config


 <system.web>

  <httpModules>
      <add name="MyPreInitModule" type="PreInitModule" />
   
    </httpModules>
 
  </system.web>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
   
      <add name="MyPreInitModule" type="PreInitModule" />
    </modules>
  <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
      <mimeMap fileExtension=".otf" mimeType="application/x-font-opentype" />
      <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
      <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
     
    </staticContent>
    <caching enabled="true" enableKernelCache="true">
      <profiles>

        <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".pdf" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".ico" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".woff2" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".eot" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
        <add extension=".ttf" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
      </profiles>
    </caching>
  </system.webServer>

1 comment:

Track your Facebook login attempt

Facebook provides a feature which allows you to see all active sessions from different devices and apps together with the date it was last...