HttpCachePolicy.AddValidationCallback(HttpCacheValidateHandler, Object) 方法

定義

註冊目前回應的驗證回呼。Registers a validation callback for the current response.

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) 的使用者提供的自定資料。The arbitrary user-supplied data that is passed back to the AddValidationCallback(HttpCacheValidateHandler, Object) delegate.

例外狀況

指定的 handlernullThe specified handler is null.

範例

下列程式碼範例示範如何加入委派,以根據查詢字串值來驗證要求。The following code example demonstrates how to add a delegate to validate a request based on query string values.

<%@ 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方法提供一種機制,可在快取中以程式設計方式檢查回應,然後再由輸出快取將回應傳回給用戶端。The AddValidationCallback method provides a mechanism to check the response programmatically in the cache before the response is returned to the client by the output cache.

從 Web 服務器快取提供回應之前,會查詢所有已註冊的處理常式,以確保資源有效。Before the response is served from the Web server cache, all registered handlers are queried to ensure resource validity. 如果有任何處理程式設定的旗標指出快取的回應無效,該專案會標示為無效,並從快取中 expelled。If any handler sets a flag indicating that the cached response is not valid, the entry is marked as not valid and expelled from the cache. 在這種情況下,以及當任何處理程式表示應該忽略此要求的快取回應時,就會像是快取遺漏一樣處理要求。In this case, as well as when any handler indicates that the cached response should be ignored for this request, the request is then handled as if it were a cache miss.

AddValidationCallback 在 .NET Framework 版本3.5 中引進。AddValidationCallback is introduced in the .NET Framework version 3.5. 如需詳細資訊,請參閱版本和相依性For more information, see Versions and Dependencies.

適用於