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)
参数
- handler
- HttpCacheValidateHandler
HttpCacheValidateHandler 值。The HttpCacheValidateHandler value.
- data
- Object
传递回 AddValidationCallback(HttpCacheValidateHandler, Object) 委托的用户提供的任意数据。The arbitrary user-supplied data that is passed back to the AddValidationCallback(HttpCacheValidateHandler, Object) delegate.
例外
指定的 handler 为 null。The 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.