Introduction to HTTP headers cached by the Asp.net client browser

Source: Internet
Author: User
Tags http authentication

To make the browser cache, you need to send a specified HTTP header to the browser to tell the browser how long it takes to cache, or never cache it. For. net Program In fact, we have been using this method. When we specify the cache location as the client in the outputcache command, it actually sends an HTTP header to the browser, tell the browser how long the URL will be cached and the last modification time.

Microsoft encapsulated the HTTP headers used in these caches in outputcachemodule, but they can be used more flexibly.

The HTTP headers related to the Client Cache are as follows:

1. expires: + expiration time
Indicates that the browser cache is invalid after the specified time. Note that the expiration time must be in the HTTP format, and others will be resolved to the current time "before ", the cache will expire immediately. The HTTP date and time must be Greenwich Mean Time (GMT) instead of the local time. Example:CopyCodeCode: expires: Fri, 30 Oct 2009 14:19:41

If you use expires to expire, the server time must be correct. Otherwise, the HTTP header sent may fail. In Windows, you can set the time server to synchronize the time.

2. cache-control:
direct translation of cache-control into Chinese indicates Cache control, which is used for Cache control. There are several HTTP header values.
1) max-age = [seconds]-execution cache is considered to be the latest longest time. Similar to the expiration time, this parameter is based on the relative time interval of the request time, instead of the absolute expiration time. [second] is a number in seconds: the number of seconds from the request time to the expiration time.
2) S-maxage = [seconds]-similar to the max-age attribute, except that it is applied to the shared (for example, proxy server) cache
3) public-the authenticated content can also be cached. Generally, the content that can be accessed only after HTTP Authentication cannot be cached automatically.
4) no-Cache-forces each request to be sent directly to the source server without the local Cache version verification. This is useful for applications that require validation (can be used together with the public), or applications that require strict use of the latest data (do not hesitate to sacrifice all the benefits of using the cache);
5) no-store-force cache do not retain any copies under any circumstances
6) must-revalidate-tells the cache that it must follow the freshness of all the copies you give, HTTP allows the cache to return expired data in certain situations. With this attribute specified, you cache the data and you want to strictly follow your rules.
7) proxy-revalidate-is similar to must-revalidate, except that it only works on the cache proxy server.
example: copy Code the code is as follows: cache-control: Max-age = 3600, must-revalidate

Obviously, cache-control can provide more flexible Cache control than expires, and it does not need to depend on server time.
In ASP.. Net microsoftwares encapsulate the settings of the cache-control attribute in the httpcachepolicy class. We can use response. cache to call the following method to control the HTTP header value of cache-control:Copy codeThe Code is as follows: Response. cachecontrol;
Response. cache. setnostore
Response. cache. setmaxage
Response. cache. setproxymaxage
Response. cache. setrevalidation

3. Last-modified/if-modified-since
The two HTTP headers are a pair. The former indicates the last update time of an address, which is the response from the server to the client. The latter is sent from the client browser to the server, tell the Web Server Client why there is a cache with the last modification time. After the server receives the IF-modified-Since Header, it determines whether the cache of the URL address cached by the client is the latest, if it is the latest, the server directly returns httpstatus 304 to the client, which means that this content has not changed since your last request, and you can directly use the cache; if the server finds that the last update time of the URL is newer than the value of IF-modified-since, new content is output.

Microsoft also encapsulates server settings for us. We can callCopy codeThe Code is as follows: Response. cache. setlastmodified (datetime)
Response. cache. setlastmodifiedfromfiledependencies ()

If you have more complex requirements, you need to handle them yourself.

4. etag/if-None-match
Etag is similar to last-modified, but it sends a string to indicate the URL version. If the URL changes, the tag also changes, when the browser sends if-None-match, it tells the browser that the content has changed or the cache can be used.

IIS automatically adds etag to the static file and generates an etag again when the file changes. This way, for more than N static files in a website, such as style sheets and small images, it is enough to download the client only once, which can reduce the load.

In ASP. NET, we can use the following two methods to setCopy codeThe Code is as follows: Response. cache. setetag (string)
Response. cache. setetagfromfiledependencies ()

Although Microsoft has made a lot of encapsulation for us, we still need to know in detail before using these HTTP headers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.