.NET OutputCache directive not working
Asked Answered
O

3

5

I've been having strange cache issues and put together a very simple .NET page with an output cache directive. However, the page is not caching (the content updates on every refresh).

I have a simple, minimal site of our CMS (Ektron v. 9.0 SP2) installed on my local machine (Windows 7). Within this website project I created a simple page for testing the output cache. Here is the page code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CacheTest.aspx.cs" Inherits="CacheTest" %>
<%@ OutputCache Duration="3600" Location="Server" VaryByParam="None" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Cache Test</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p><%= DateTime.Now.ToString() %></p>
    </div>
    </form>
</body>
</html>

This page does not cache at all.

On our production site, the OutputCache in general does not work either, except on one test page which is configured exactly like the above. I cannot figure out why that one page is different, and is the only one that works on the dev server but when copied into the site running on my localhost, it does not work anymore.

I did notice that on our production site, using a master page seems to make the output cache not work, though in this localhost site I am not using a master page and it still does not work.

Where should I begin looking to troubleshoot this issue? I've looked through IIS settings and can't find any obvious settings to turn on/off page-level caching. I've also searched the web extensively and can't seem to find anyone else with this issue.

Overlap answered 20/4, 2015 at 15:5 Comment(0)
L
5

Microsoft has disabled output cache for pages that include cookies to prevent one user from getting a cached version of a page that was meant for another user.

There is a workaround that removes cookies from the output. See https://support.episerver.com/hc/en-us/articles/115004119986-Output-Caching-Broken

As an alternative, you could also use partial page caching by caching your user controls.

Luckett answered 21/4, 2015 at 12:8 Comment(4)
I tried the workaround in the link and it worked perfectly, although I had to add a test to check if HttpContext.Current is null in the PreSendRequestHeaders method to avoid getting exceptions while debugging. The cookies could explain why it works in some situations and not others on the production website, as well.Overlap
The workaround that that link fixed the issue, but I can't find any official explanation about why MS did that or whether this is a risky fix or not. Any ideas?Teeny
You only want to use page output caching when you are serving the same page to anyone who visits your site. If you are serving any custom content based on a cookie or session variable then you do not want to use this code. The danger is that if you serve different content to people based on their cookie, you may serve the wrong content to the second person who accesses the page.Luckett
Link is broken, does anyone know what is the workaround ?Astrograph
S
0

Add the CacheProfile attribute in your definition,

<%@ OutputCache CacheProfile="CacheOneHour" Duration="3600" Location="Server" VaryByParam="none" %>

Declare the cache profile in your Web.config file. (within the system.web, place your declaration):

  <system.web>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="CacheOneHour"
               duration="3600"
               location="Server"
               varyByParam="none" />
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
Somerset answered 21/4, 2015 at 2:24 Comment(0)
L
0

I encountered a similar problem. The problem was that caching was stopped on Base page.

public static void StopCachingOfPage()
        {
            // Stop Caching in IE 
            HttpContext.Current.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
            // Stop Caching in Firefox 
            HttpContext.Current.Response.Cache.SetNoStore();
        }

So just look for this in you code. In case you need the caching override this call.

Luxor answered 15/5, 2017 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.