This tutorial will show numerous options of caching
pages, how we can create different versions of cached pages depending
upon different factors. VB version.
Using VaryBy attribute in OutputCache in ASP.NET 3.5 VB
This tutorial will show numerous options of caching
pages, how we can create different versions of cached pages depending
upon different factors. VB version.
The output cache is used to ease the load on the server by
saving versions of pages in memory, instead of compiling pages every
time they are requested by users. ASP.NET 3.5 gives us plenty of
options to configure caching to fit our needs. This tutorial will
explain some ways in which we can vary our output cache.
When using Output Cache, we use the following Directive at the top of our ASPX page, which looks something like this:
| <%@ OutputCache Duration="2000" VaryByParam="none" %> |
Varying the Output Cache by Header
Using this
attribute in the OutputCache directive will create different versions
of a page depending upon the value of the particular browser header.
The standard browser headers that are transmitted include
Accept-Language, which is a list of languages that the client making
the request prefers; User-Agent, which is the type of device making the
page request; and Cookie, which are the browser cookies created in the
current domain.
Using the Accept-Language would look something like this:
| <%@ OutputCache Duration="2000" VaryByParam="none" VaryByHeader="Accept-Language" %> |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers
and add-ons were setup swiftly, in less than 24 hours. We were able to
confirm our order over the phone. They respond to our inquiries within
an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
We can also use the User-Agent value to create different cached
pages depending upon the type of browser used. This directive would
look something like this:
| <%@ OutputCache Duration="15" VaryByParam="none" VaryByHeader="User-Agent" %> |
We can also see what value the User-Agent is with the following VB code:
| Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = DateTime.Now.ToString("T") & "<br /><br />" & Request.UserAgent.ToString()
End Sub |
We use the time to see if we are viewing a cached page or a
generated one, and the UserAgent shows us what the header sees. It is
often impractical to use the UserAgent value when using
VaryOutputByHeader, as there are so many variations of the string. This
results in many different cached versions of the page.
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
Varying Output Cache by Browser
One way of
getting roung the issue mentioned above is to use the VaryByCustom
attribute of the OutputCache directive, and use the browser value. We
would do this by using the following:
| <%@ OutputCache Duration="15" VaryByParam="none" VaryByCustom="browser" %> |
The above directive would create cached pages of different browser types, and major versions.
Varying the Output Cache by Custom Function
We
can also create our own functions to vary caching by. We can do this by
using the VaryByCustom attribute of the directive and using our own
custom string and then modifying the Global.asax file. For example, if
we wanted to create different cached pages for browsers that supported
CSS and those that didn't, then we could add the following method into
the Global.asax file:
Public Overrides Function GetVaryByCustomString(ByVal context As HttpContext, ByVal custom As String) As String
If String.Compare(custom, "css") = 0 Then
Return Request.Browser.SupportsCss.ToString()
End If
Return MyBase.GetVaryByCustomString(context, custom)
End Function |
|