.NET OutputCache directive not working

青春壹個敷衍的年華 提交于 2019-12-23 13:01:07

问题


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.


回答1:


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.




回答2:


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.




回答3:


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>


来源:https://stackoverflow.com/questions/29751570/net-outputcache-directive-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!