Compartilhar via


How to: Check the Validity of a Cached Page

When a cached page is requested by a user, ASP.NET determines whether the cached output is still valid based on the cache policy you have defined in the page. If the output is valid, the cached output is sent to the client and the page is not re-processed. However, ASP.NET provides you with the ability to run code during this validation check using a validation callback, so that you can write custom logic to check whether the page is valid. The validation callback allows you to invalidate cached pages outside of the normal process of using cache dependencies.

To programmatically check the validity of a cached page

  1. Define an event handler of type HttpCacheValidateHandler and include code that checks the validity of the cached page response.

    The validation handler must return one of the following HttpValidationStatus values:

    • Invalid   Indicates that the cached page is invalid, the page is evicted from the cache, and the request is handled as a cache miss.

    • IgnoreThisRequest   Causes the request to be treated as a cache miss. The page is therefore processed again, but the cached page is not invalidated.

    • Valid   Indicates that the cached page is valid.

    The following code example illustrates a validation handler named ValidateCacheOutput that determines whether the query string variable status contains the values "invalid" or "ignore". If the status value is "invalid", the method returns Invalid and the page is invalidated in the cache. If the status value is "ignore", the method returns IgnoreThisRequest and the page is left in the cache but a new response is generated for this request.

    public static void ValidateCacheOutput(HttpContext context, Object data,
            ref HttpValidationStatus status)
    {
        if (context.Request.QueryString["Status"] != null)
        {
            string pageStatus = context.Request.QueryString["Status"];
    
            if (pageStatus == "invalid")
                status = HttpValidationStatus.Invalid;
            else if (pageStatus == "ignore")
                status = HttpValidationStatus.IgnoreThisRequest;
            else
                status = HttpValidationStatus.Valid;
        }
        else
            status = HttpValidationStatus.Valid;
    }
    
    Public Shared Sub ValidatePage(ByVal context As HttpContext, _
            ByVal data As [Object], ByRef status As HttpValidationStatus)
        If Not (context.Request.QueryString("Status") Is Nothing) Then
            Dim pageStatus As String = context.Request.QueryString("Status")
    
            If pageStatus = "invalid" Then
                status = HttpValidationStatus.Invalid
            ElseIf pageStatus = "ignore" Then
                status = HttpValidationStatus.IgnoreThisRequest
            Else
                status = HttpValidationStatus.Valid
            End If
       Else
           status = HttpValidationStatus.Valid
       End If
    End Sub
    
  2. From one of the page life-cycle events (such as the page's Load event), call the AddValidationCallback method, passing as the first argument the event handler you defined in step 1.

    The following code example sets the ValidateCacheOutput method to be the validation handler.

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.AddValidationCallback(
            new HttpCacheValidateHandler(ValidateCacheOutput),
            null);
    }
    
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles Me.Load
    
        Response.Cache.AddValidationCallback( _
            New HttpCacheValidateHandler(AddressOf ValidatePage), Nothing)
    End Sub
    

See Also

Concepts

Caching ASP.NET Pages