0

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>
        &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>

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • When you say "it doesn't work", do you mean the date is always updated on the page (i.e. it isn't cached), or is there another problem (an exception?) – Graham Clark Aug 12 '11 at 13:22
  • @Graham Clark hi : mean the date is always updated on the page (i.e. it isn't cached), – SilverLight Aug 12 '11 at 13:24
  • also i have no problem with page directive for outputCaching / but prorammatically ! / should i change something in machine.config or root web.config or application web.config for getting affects ? – SilverLight Aug 12 '11 at 13:26
  • see @m.edmondson's answer. You seem to be assuming that the output caching page directive is the same as the code you've written in the `Page_Load` method. This is incorrect. – Graham Clark Aug 12 '11 at 13:37

1 Answers1

1
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.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • thanks for answer / i test the upper codes in different browsers without any result (mean by clicking refresh button in all of them time is changed , but should n't be) / would u plz let us to know that these codes work for you or not ? – SilverLight Aug 12 '11 at 13:39
  • The thing is, by pressing refresh most browsers functionality is to specifically request the page again (so the time WILL be updated) – m.edmondson Aug 12 '11 at 13:41
  • i edit my Q / by pressing refresh most browsers functionality is to specifically request the page again (so the time WILL be updated) -> but should n't! – SilverLight Aug 12 '11 at 13:51
  • by UnComment //Response.Cache.VaryByParams.IgnoreParams = true; line those codes work perfect / however i marked your answer as my answer because of taking your time on it...thanks a lot for your answer and attention and introducing Fiddler. – SilverLight Aug 12 '11 at 14:46
  • Sorry are you saying you put `Response.Cache.VaryByParams.IgnoreParams = true;` in? – m.edmondson Aug 12 '11 at 14:47
  • The [docs](http://msdn.microsoft.com/en-us/library/system.web.httpcachevarybyparams.ignoreparams.aspx) answers that. You're telling asp.net to ignore parameters passed to it, so in turn can cache the page – m.edmondson Aug 12 '11 at 14:56