Response.Cache.SetCacheability不起作用

本文关键字:不起作用 SetCacheability Cache Response | 更新日期: 2023-09-27 18:16:44

在尝试使用Response.Cache.SetCacheability()缓存页面时,我调试了程序(F5)。然而,我无法得到这个工作。每次单击button1后,Lable1文本都会立即更新。

代码隐藏文件:

Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(1000));
Response.Cache.SetValidUntilExpires(true);
label1.Text = " Using HTTP CachePolicy class" + DateTime.Now.ToString();

这是我的。aspx页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<asp:Label runat="server" Text="Label" ID="label1"></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Content>

请帮助。我甚至无法理解这个非常基本的例子!

Response.Cache.SetCacheability不起作用

单击该按钮将导致POST,该POST将始终转到服务器,即使页面被缓存

我们还需要以编程方式添加这一行:

Response.Cache.VaryByParams.IgnoreParams = true; // in case we are not using any VaryByParams parameter.

这里我没有使用任何参数来改变缓存。因此,在将上述代码行添加到我的问题之后,它开始工作了。

我还观察到,在声明性标记中,如果省略VaryByParams,则会抛出错误。这行不通。

<%@ OutputCache Duration="60" %>

但这将工作:

<%@ OutputCache Duration="60" VaryByParam="None" %>