HttpCachePolicy.SetAllowResponseInBrowserHistory(Boolean) メソッド

定義

allow パラメーターがtrueされている場合、サーバーで行われたHttpCacheability設定に関係なく、クライアント ブラウザーの履歴キャッシュで応答を使用できるようにします。

public:
 void SetAllowResponseInBrowserHistory(bool allow);
public void SetAllowResponseInBrowserHistory(bool allow);
member this.SetAllowResponseInBrowserHistory : bool -> unit
Public Sub SetAllowResponseInBrowserHistory (allow As Boolean)

パラメーター

allow
Boolean

true クライアントブラウザに履歴フォルダに応答を格納するように指示する。それ以外の場合は false。 既定値は false です。

次のコード例では、 SetAllowResponseInBrowserHistory メソッドをオーバーライドして、カスタム HttpCachePolicyの履歴に応答を格納するようにクライアントに指示する方法を示します。

<%@ Page language="c#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>HttpCachePolicy - SetAllowResponseInBrowserHistory - C# Example</title>
    <script runat="server">
      void Page_Load(Object sender, EventArgs e) 
      {
        // When HttpCacheability is set to NoCache or ServerAndNoCache 
        // the Expires HTTP header is set to -1 by default. This instructs 
        // the client to not cache responses in the History folder. Thus, 
        // each time you use the back/forward buttons, the client requests 
        // a new version of the response. 
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
        
        // Override the ServerAndNoCache behavior by setting the SetAllowInBrowserHistory 
        // method to true. This directs the client browser to store responses in  
        // its History folder.
        HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(true);
        
        // Display the DateTime value.
        Label1.Text = DateTime.Now.ToLongTimeString();
      }
    </script>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>HttpCachePolicy - SetAllowResponseInBrowserHistory - C# Example</h3>
      
      <p>Click the Submit button a few times, and then click the Browser's Back button.<br />
        The page should be stored in the Browser's History folder</p>
        
      <p>Time: <asp:Label id="Label1" runat="server" Font-Bold="True" ForeColor="Red" /></p>
      
      <asp:Button id="Button1" runat="server" Text="Submit" />
    </form>
  </body>
</html>
<%@ Page language="VB" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>HttpCachePolicy - SetAllowResponseInBrowserHistory - Visual Basic .NET Example</title>
    <script runat="server">
      Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
          
          ' When HttpCacheability is set to NoCache or ServerAndNoCache 
          ' the Expires HTTP header is set to -1 by default. This instructs 
          ' the client to not cache responses in the History folder. Thus, 
          ' each time you use the back/forward buttons, the client requests 
          ' a new version of the response. 
          HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)
          
          ' Override the ServerAndNoCache behavior by setting the SetAllowInBrowserHistory 
          ' method to true. This directs the client browser to store responses in  
          ' its History folder.
          HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(True)
          
          ' Display the DateTime value.
          Label1.Text = DateTime.Now.ToLongTimeString()
      End Sub
    </script>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">
      <h3>HttpCachePolicy - SetAllowResponseInBrowserHistory - Visual Basic .NET</h3>
      
      <p>Click the Submit button a few times, and then click the Browser's Back button.<br />
        The page should be stored in the Browser's History folder</p>
        
      <p>Time: <asp:Label id="Label1" runat="server" Font-Bold="True" ForeColor="Red" /></p>
      
      <asp:Button id="Button1" runat="server" Text="Submit" />
    </form>
  </body>
</html>

注釈

HttpCacheabilityNoCacheに設定されている場合、またはServerAndNoCache場合、Expires HTTP ヘッダーは既定で -1 に設定されます。これにより、クライアントは履歴フォルダーに応答をキャッシュしないように指示するため、戻る/転送ボタンを使用すると、クライアントは毎回新しいバージョンの応答を要求します。 この動作をオーバーライドするには、allow パラメーターを true に設定してSetAllowResponseInBrowserHistory メソッドを呼び出します。

HttpCacheabilityNoCacheまたはServerAndNoCache以外の値に設定されている場合、allowのいずれかの値を使用してSetAllowResponseInBrowserHistory メソッドを呼び出しても効果はありません。

SetAllowResponseInBrowserHistory は、.NET Framework バージョン 3.5 で導入されています。 詳細については、「 バージョンと依存関係」を参照してください。

適用対象