ASP.NET 以编程方式启用输出缓存不起作用 ->为什么?

发布于 2024-11-29 01:27:35 字数 2751 浏览 2 评论 0原文

为什么在下面的 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();
}

那么以编程方式存在什么问题呢?

why, in the below aspx and code behind, when Output Caching is enabled programmatically (enabled in code behind), it does not work and has a problem?

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>

code behind:

    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();
}

with page directive for output caching there is no problem:
mean
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>
         <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>

and
code behind:

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

so what is the problem about programmatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-12-06 01:27:35
Response.Cache

所有这些方法所做的就是修改响应中的 HTTP 标头,要求浏览器执行某些操作(在本例中修改其缓存方式)。

您使用过Fiddler 来查看这些吗?

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

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

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

Response.Cache

All these methods do is modifiy the HTTP headers in the response which ask the browser to do something (in this case modifiy how it cache's).

Have you used Fiddler to see these?

I would guess that ASP.net has changed the last-modified date (since it knows the time has changed), however there's a few reasons why the browser would still update:

  • The browser could have caching disabled
  • The cache could have just been cleared
  • It is using some other method to decide the page needed refreshed from the server
  • The browser can request whatever it wants

I suggest you research some of these points, however you definitely shouldn't be relying on the browsers cache to ensure the functionality of your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文