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) 委托的用户提供的任意数据。

例外

指定的 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 中引入。 有关详细信息,请参见版本和依赖关系

适用于