Reputation: 10565
While trying to cache a page using Response.Cache.SetCacheability(), I debug the program (F5) . However I'm unable to get this to work. Each time after clicking the button1, the Lable1 text is updated instantaneously.
Code Behind file:
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();
Here is my .ASPX page:
<%@ 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>
Please help. I'm even unable to get this very basic example!
Upvotes: 2
Views: 2310
Reputation: 10565
we also need to add this line programmatically:
Response.Cache.VaryByParams.IgnoreParams = true; // in case we are not using any VaryByParams parameter.
Here I wasn't using any parameters for varying the cache. So after putting the above line of code in addition to my question, it started working .
I also observed that in declarative markup, if we omit VaryByParams
then it will throw errors. This will never work.
<%@ OutputCache Duration="60" %>
But this will work:
<%@ OutputCache Duration="60" VaryByParam="None" %>
Upvotes: 1
Reputation: 124804
Clicking the button will cause a POST, which will always go to the server even if the page is cached.
Upvotes: 2