为什么,在下面的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();
}
使用page指令进行输出缓存没有问题:
意味着
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>
和
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
lblDate.Text = "The time is now:<br>";
lblDate.Text += DateTime.Now.ToString();
}
那么以编程方式出现的问题是什么?
答案 0 :(得分:1)
Response.Cache
所有这些方法都修改了响应中的HTTP标头,要求浏览器执行某些操作(在这种情况下修改它的缓存方式)。
您是否使用Fiddler来查看这些内容?
我猜想ASP.net已经改变了上次修改日期(因为它知道时间已经改变),但是有几个原因导致浏览器仍然会更新:
我建议您研究其中的一些观点,但是您绝对不应该依赖浏览器缓存来确保应用的功能。