ASP.NET Tutorial/Development/OutputCache

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

OutputCache Duration="30" VaryByParam=true (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <%@ OutputCache Duration="30" VaryByParam="true"%> <script runat="server">

  void Page_Load(Object Sender, EventArgs e) {
     lblMessage.Text = "Welcome " + Request.Params["id"] + "! The time is now " + DateTime.Now.ToString("T");
  }

</script> <html><body>

  <asp:Label id="lblMessage" runat="server"/>

</body></html></source>


Set VaryByParam (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <%@ OutputCache Duration="60" VaryByParam="none" %> <script runat="server">

  sub Page_Load(Sender as Object, e as EventArgs)
     Response.Cache.VaryByParams.Item("first") = true
     Response.Cache.VaryByParams.Item("last") = false
     lblMessage.Text = "Welcome " & Request.Params("first") & _
        "! The time is now " & DateTime.Now.ToString("T")
     lblMessage.Text += "
" & Response.Cache. _ VaryByParams.Item("first").ToString lblMessage.Text += "
" & Response.Cache. _ VaryByParams.Item("last").ToString end sub

</script> <html><body>

  <form runat="server">
     <asp:Label id="lblMessage" runat="server"/>
  </form>

</body></html></source>


Using the Output Cache (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <%@ OutputCache Duration="30" VaryByParam="none" %> <script runat="server">

  void Page_Load(Object Sender, EventArgs e) {
     lblMessage.Text = "Welcome! The time is now " + DateTime.Now.ToString("T");
  }

</script> <html><body>

  <asp:Label id="lblMessage" runat="server"/>

</body></html></source>