28.If-Not-Modified-Since, VaryByCustom, Output Caching
82. How does if-not-modified-since work? How can it be programmatically implemented with ASP.NET? Explain <@OutputCache%> and the usage of VaryByParam, VaryByHeader.
If-modified-since request header field is used with a method to make it conditional: if the requested variant has not been modified since the time specified in this field, an entity will not be returned from the server, instead, a 304(not modified) response will be returned without any message body. The purpose of this feature is to allow efficient updates of cached information with a minimum of transaction overhead. OuputCache in ASP.NET handles this process.
<@OutputCache%> is for declarative configuration of caching for a single page. Adding the <@OutputCache%> directive to the top of a pages markup enables granular control of how the page gets cached. Some @OutputCache attributes include: Duration, Location, CacheProfile, NoStore, Shared, VaryByParam, VaryByControl, SqlDependency, VaryByCustom and VaryByHeader. This examples demonstrates how to cache a page for 30 minutes, notwithstanding the number of the parameters passed to the page:
<%@ OutputCache Duration=”30″ VaryByParam=”none” %>
<!–OutputCache Duration="30" VaryByParam=”none”–>
VaryByParam consists of a semi-colon separated list of strings used to vary the output cache. These strings correspond to a query string value sent with the GET method or a parameter sent with the POST method. VaryByControl consists of a semi-colon separated list of strings used to vary a user control’s output cache. These strings represent the ID property values of ASP.NET server controls declared in the user control. VaryByHeader consists of a semi-colon separated list of HTTP headers used to vary the output cache. When this attribute is sent to multiple headers, the output cache contains a different version of the requested document for each combination of specified headers.
83. How does VaryByCustom work?
VaryByCustom: any text that represents custom output caching requirements. If this attribute is given a value of browser, the cache is varied by browser name and major version information. If a custom string is entered, you must override the GetVaryByCustomString method in the Global.asax file.
84. How would one implement ASP.NET HTML output caching, caching outgoing versions of pages generated via all values of q = except where q = 5(as in http://localhost/page.aspx?q=5 ?
HTTPPolicy.AddValidationCallback method. Registers a validation callback for the current response. 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. Before the response is served from the Web Server Cache, all registered handlers are queried to ensure resource validity. 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 is introduced in the .NET Framework version 3.5.
Happy Programming! =)
Source: mainly MSDN.