.NET OutputCache directive not working

2019-06-25 12:46发布

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.

3条回答
戒情不戒烟
2楼-- · 2019-06-25 12:53

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>
查看更多
冷血范
3楼-- · 2019-06-25 12:59

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.

查看更多
趁早两清
4楼-- · 2019-06-25 13:10

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.

查看更多
登录 后发表回答