This documentation is archived and is not being maintained.

HttpCachePolicy.AddValidationCallback Method

Registers a validation callback for the current response.

[Visual Basic]
Public Sub AddValidationCallback( _
   ByVal handler As HttpCacheValidateHandler, _
   ByVal data As Object _
public void AddValidationCallback(
 HttpCacheValidateHandler handler,
 object data
public: void AddValidationCallback(
 HttpCacheValidateHandler* handler,
 Object* data
public function AddValidationCallback(
   handler : HttpCacheValidateHandler,
 data : Object


The HttpCacheValidateHandler value.
The arbitrary user-supplied data that is passed back to the AddValidationCallback delegate.


Exception Type Condition
ArgumentNullException handler is a null reference (Nothing in Visual Basic).


AddValidationCallback provides a mechanism to programmatically check the validity of a item in the cache before the item is returned from the cache.

Before the response is served from the Web server cache, all registered handlers are queried to ensure resource validity. If any handler sets a flag indicating that the resource is invalid, the entry is marked invalid and evicted from the cache. The request is then handled as if it were a cache miss.


[Visual Basic] 
<%@ Page Language="VB" %>
<%@ OutputCache VaryByParam="none" Duration=600 %>
<Script runat="server">
   shared validationstate As String

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

   Public Sub Validate(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
         status = HttpValidationStatus.Valid
      End If
   End Sub


<%@ OutputCache VaryByParam="none" Duration=600 %>
<Script Language="C#" runat="server">
   static string validationstate;
   public void Page_Load()
      Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(Validate), null);
      stamp.InnerHtml = DateTime.Now.ToString("r");

   public void Validate(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;
         status = HttpValidationStatus.Valid;


[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

HttpCachePolicy Class | HttpCachePolicy Members | System.Web Namespace