The cacheability of pages or user controls defines what HTTP 1.1 Internet devices can cache documents. These devices include the client sending the request, the Web server responding to the request, and any device (such as a proxy server) with cache functions in the request or response stream ). When the Web server sends a response, it includesCache-ControlHTTP header, which defines the devices on which documents can be cached. ASP. NET defines these values in the HttpCacheability enumeration. This enumeration has six values, four of which are directly mappedCache-ControlHTTP header settings. The other two special values areHttpCacheability. ServerAndPrivateAndHttpCacheability. ServerAndNoCache.ServerAndPrivateOnly the response can be cached on the original server and request client. WhileServerAndNoCacheOnly the response can be cached on the original server.
Depending on the application requirements, you can select which devices can cache specific pages and which devices cannot cache specific pages. For example, you may want the cache settings on the user login page to be different from the directory page selected by the product to be displayed. For security reasons, you may want to cache only the pages on the server, while the directory pages can be cached on any device that supports caching.
By addingLocationAttributes are included in the @ OutputCache command and an OutputCacheLocation enumeration value is specified. You can set the page output cacheability in declaration mode.
Use the HttpCachePolicy. SetCacheability method to specifyHttpCacheabilityValue, You can programmatically set the page output cache. You can access this method through the HttpResponse. Cache attribute, and use the code of the page to hide the class or declareResponse. CacheThe syntax can access this attribute.
Note:If you use@ OutputCacheYou mustLocationAttribute Declaration togetherDurationAndVaryByParamAttribute. You must setDurationSet the property to a value greater than zero. If you do not want to useVaryByParamYou can set the attributeNone. For more information, see set the expiration time of the page cache and multiple versions of the cache page.
In contrastHttpCachePolicyClass sets the cache. If a verification policy has been established, you do not need to set the expiration time.
Set page cache in declarative Mode
- Set@ OutputCacheThe command is included in the. aspx file and the requiredDurationAndVaryByParamAttribute.
- SetLocationAttributes are included in@ OutputCacheCommand and define its value as one of the supported values. These values includeAny,Client,Downstream,ServerOrNone.
Note:The default value isAny. If Not DefinedLocationThe page output can be cached on all network applications with cache functions related to the response. This includes the request client, the original server, and any proxy server that responds to the request.
Set page cache by programming
- In the Code declaration block or hidden class file on the page, useResponse. CacheTo access the HttpCachePolicy. SetCacheability method. The following code willCache-ControlSet the HTTP headerPublic.
[C#] Response.Cache.SetCacheability(HttpCacheability.Public);[Visual Basic] Response.Cache.SetCacheability(HttpCacheability.Public)
Note:If you set the cacheabilityHttpCacheability. NoCacheOrHttpCacheability. ServerAndNoCacheThe requested client will not cache the page in its "History" folder. For example, when a user clicks the backward or forward button, the user requests a new version of the response. Set HttpCachePolicy. SetAllowResponseInBrowserHistoryTrue, You can override this behavior.
[C#] Response.Cache.SetAllowResponseInBrowserHistory(true);[Visual Basic] Response.Cache.SetAllowResponseInBrowserHistory(true)
If you set the cacheabilityNoCacheOrServerAndNoCacheAny other value, ASP. NET will ignoreSetAllowResponseInBrowserHistoryThe value set by the method.