共用方式為


How to: Set a Page's Cacheability Programmatically

The cacheability of a page or user control refers to whether a page can be cached on a device during the page's response life cycle. Devices that can cache a page include the browser making the request, the Web server responding to the request, and any cache-capable devices, such as proxy servers, that are in the request or response stream.

You can set cacheability programmatically if your application will determine cacheability based on run-time conditions, such as reading the request header. For more information, see Setting the Cacheability of a Page.

To set a page's cacheability programmatically

  • In the page's code, call the SetCacheability method on the Cache property of the Response object.

    The following code sets the Cache-Control HTTP header to Public.

    Response.Cache.SetCacheability(HttpCacheability.Public);
    
    Response.Cache.SetCacheability(HttpCacheability.Public)
    

    If you pass either NoCache or ServerAndNoCache to the SetCacheability method to prevent a requesting browser from caching a page in its History folder, any time a user clicks a back or forward button, a new version of the response will be requested. You can override this behavior conditionally by calling the SetAllowResponseInBrowserHistory method on the Cache property and passing true for the allow parameter.

    If you set cacheability to any value other than NoCache or ServerAndNoCache, ASP.NET ignores the value set by the SetAllowResponseInBrowserHistory method.

See Also

Tasks

How to: Set the Cacheability of an ASP.NET Page Declaratively

Concepts

Caching ASP.NET Pages
Setting the Cacheability of a Page