title: With ASP.Net, how do I enable browser caching for static content and disable it for dynamic content?
body: <p>I have found a lot of good information with regards to getting browsers to avoid caching dynamic content (e.g. .aspx pages), however I have not been successful with getting browsers to cache my static content, specifically css, javascript and image files. </p>
  
  <p>I have been playing with Application_BeginRequest in Global.asax without success. Having a separate server for static content is not an option for us. I would also like to avoid having to configure IIS settings unless they can be controlled from the web.config. Could disabling caching for an aspx page influence the caching of static content that appears on it?</p>
  
  <p>I apologise if this question has been answered previously. </p>
  
  <p>As a starting point for discussion, here is the code behind for my Global.asax file.</p>
  
  <pre><code>public class Global_asax : System.Web.HttpApplication
  {
      private static HashSet&lt;string&gt; _fileExtensionsToCache;
  
      private static HashSet&lt;string&gt; FileExtensionsToCache
      {
          get 
          {
              if (_fileExtensionsToCache == null) 
              {
                  _fileExtensionsToCache = new HashSet&lt;string&gt;();
  
                  _fileExtensionsToCache.Add(".css");
                  _fileExtensionsToCache.Add(".js");
                  _fileExtensionsToCache.Add(".gif");
                  _fileExtensionsToCache.Add(".jpg");
                  _fileExtensionsToCache.Add(".png");
              }
  
              return _fileExtensionsToCache;
          }
      }
  
      public void Application_BeginRequest(object sender, EventArgs e)
      {
          var cache = HttpContext.Current.Response.Cache;
  
          if (FileExtensionsToCache.Contains(Request.CurrentExecutionFilePathExtension)) 
          {
              cache.SetExpires(DateTime.UtcNow.AddDays(1));
              cache.SetValidUntilExpires(true);
              cache.SetCacheability(HttpCacheability.Private);
          } 
          else 
          {
              cache.SetExpires(DateTime.UtcNow.AddDays(-1));
              cache.SetValidUntilExpires(false);
              cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
              cache.SetCacheability(HttpCacheability.NoCache);
              cache.SetNoStore();
          }
      }
  }
  </code></pre>