【问题标题】:ASP.NET Programmatically enabling Output Caching does not work -> Why?ASP.NET 以编程方式启用输出缓存不起作用 -> 为什么?
【发布时间】:2011-08-12 13:14:51
【问题描述】:

为什么,在下面的 aspx 和后面的代码中,当以编程方式启用输出缓存(在后面的代码中启用)时,它不起作用并且有问题?

aspx:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="ProgrammaticOutputCaching"
    CodeBehind="ProgrammaticOutputCaching.aspx.cs" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblDate" runat="server" Font-Bold="False" Font-Names="Verdana" Font-Size="XX-Large"></asp:Label><br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Refresh" />
    </div>
    </form>
</body>
</html>

后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.Public);

        // Use the cached copy of this page for the next 60 seconds.
        Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
        //Response.Cache.VaryByParams.IgnoreParams = true;

        // This additional line ensures that the browser can't
        // invalidate the page when the user clicks the Refresh button
        // (which some rogue browsers attempt to do).
        Response.Cache.SetValidUntilExpires(true);

        lblDate.Text = "The time is now:<br>" + DateTime.Now.ToString();
}

使用用于输出缓存的页面指令没有问题:
意思是
aspx:

<%@ Page Language="C#" AutoEventWireup="true" Inherits="OutputCaching" CodeBehind="OutputCaching.aspx.cs" %>

<%@ OutputCache Duration="60" VaryByParam="Name;Age" Location="Server" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:Label ID="lblDate" runat="server" Font-Bold="False" Font-Names="Verdana"
            Font-Size="XX-Large"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Refresh" />
    </div>
    </form>
</body>
</html>

和
背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
        lblDate.Text = "The time is now:<br>";
        lblDate.Text += DateTime.Now.ToString();
}

那么以编程方式有什么问题?

【问题讨论】:

  • 当您说“它不起作用”时,您的意思是页面上始终更新日期(即不缓存),还是有其他问题(例外?)
  • @Graham Clark hi : 意味着日期总是在页面上更新(即它没有被缓存),
  • 我也对 outputCaching 的页面指令没有任何问题 / 但在程序上! / 我应该更改 machine.config 或 root web.config 或应用程序 web.config 中的某些内容以获得影响吗?
  • 见@m.edmondson 的回答。您似乎假设输出缓存页面指令与您在 Page_Load 方法中编写的代码相同。这是不正确的。

标签: c# visual-studio-2010 asp.net-4.0 output-caching


【解决方案1】:
Response.Cache

所有这些方法所做的都是修改响应中的 HTTP 标头,这些标头要求浏览器执行某些操作(在这种情况下,修改它的缓存方式)。

你用Fiddler看过这些吗?

我猜想 ASP.net 已经改变了最后修改日期(因为它知道时间已经改变),但是浏览器仍然会更新有几个原因:

  • 浏览器可能禁用了缓存
  • 缓存可能刚刚被清除
  • 它正在使用其他方法来决定需要从服务器刷新的页面
  • 浏览器可以任意请求

我建议您研究其中的一些要点,但是您绝对不应该依赖浏览器缓存来确保您的应用程序的功能。

【讨论】:

  • 感谢您的回答/我在不同的浏览器中测试了上面的代码,没有任何结果(意味着通过在所有这些浏览器中单击刷新按钮,时间都改变了,但不应该)/请你让我们知道这些代码对你有用吗?
  • 问题是,通过按刷新大多数浏览器的功能是专门再次请求页面(所以时间会被更新)
  • 我通过按刷新来编辑我的 Q / 大多数浏览器的功能是再次专门请求页面(因此时间将被更新)-> 但不应该!
  • 取消注释 //Response.Cache.VaryByParams.IgnoreParams = true;这些代码完美无缺/但是我将您的答案标记为我的答案,因为您花时间在上面...非常感谢您的回答和关注并介绍 Fiddler。
  • 对不起,你是说你把Response.Cache.VaryByParams.IgnoreParams = true;放进去了?
猜你喜欢
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 2012-07-21
  • 2023-03-08
  • 2011-03-30
  • 1970-01-01
  • 2011-10-27
相关资源
最近更新 更多