共用方式為


HttpCachePolicy.AddValidationCallback 方法

定義

註冊目前回應的驗證回呼。

public:
 void AddValidationCallback(System::Web::HttpCacheValidateHandler ^ handler, System::Object ^ data);
public void AddValidationCallback (System.Web.HttpCacheValidateHandler handler, object data);
member this.AddValidationCallback : System.Web.HttpCacheValidateHandler * obj -> unit
Public Sub AddValidationCallback (handler As HttpCacheValidateHandler, data As Object)

參數

data
Object

傳回 AddValidationCallback(HttpCacheValidateHandler, Object) 委派 (Delegate) 的使用者提供的自定資料。

例外狀況

指定的 handlernull

範例

下列程式碼範例示範如何新增委派,以根據查詢字串值來驗證要求。

<%@ Page Language="C#" %>
<%@ OutputCache VaryByParam="none" Duration="600" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="c#" runat="server">

   static string validationstate;
 
   public void Page_Load()
   {
      Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(ValidateCache), null);
      stamp.InnerHtml = DateTime.Now.ToString("r");
   }

   public static void ValidateCache(HttpContext context, Object data, ref HttpValidationStatus status) 
   {
      if (context.Request.QueryString["Valid"] == "false") 
      {
         status = HttpValidationStatus.Invalid;
      } 
      else if (context.Request.QueryString["Valid"] == "ignore") 
      {
         status = HttpValidationStatus.IgnoreThisRequest;
      } 
      else 
      {
         status = HttpValidationStatus.Valid;
      }
   }

</script>
<%@ Page Language="VB" %>
<%@ OutputCache VaryByParam="none" Duration="600" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
   shared validationstate As String

   Public Sub Page_Load(sender As Object, e As EventArgs)
      Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(AddressOf Me.ValidateCache), nothing)
      stamp.InnerHtml = DateTime.Now.ToString("r")
   End Sub

   Public Shared Sub ValidateCache(context As HttpContext, data As Object, ByRef status as HttpValidationStatus)
      If (context.Request.QueryString("Valid") = "false") Then
         status = HttpValidationStatus.Invalid
      Elseif (context.Request.QueryString("Valid") = "ignore") Then
         status = HttpValidationStatus.IgnoreThisRequest
      Else
         status = HttpValidationStatus.Valid
      End If
   End Sub

</script>

備註

方法 AddValidationCallback 提供一種機制,以程式設計方式檢查快取中的回應,然後輸出快取才會將回應傳回用戶端。

從 Web 服務器快取提供回應之前,系統會查詢所有已註冊的處理常式,以確保資源有效性。 如果有任何處理程式設定旗標,指出快取的回應無效,則專案會標示為無效,並從快取中擷取。 在此情況下,以及任何處理程式指出應該忽略此要求的快取回應時,就會處理要求,就像快取遺漏一樣。

AddValidationCallback.NET Framework 3.5 版引進。 如需詳細資訊,請參閱版本和相依性

適用於