Caching is a very easy solution to many performance related issues on almost any website. There are many different ways to use server-side caching and they all have their own unique advantages. But client-side caching is often ignored in the .NET blogosphere even though it is just as important as the server-side cache.

Everywhere server-side caching is used you can and should use client-side caching as well. Even though the server can serve cached ASP.NET pages very fast it still rely on the browser to download and render the output. When adding client-side caching you get an enormous performance benefit when the page is visited more than once by the same browser.

Firefox and IE6/7 has a slightly different way of interpret client-side caching, so you have to implement it correct to get it to work in both browsers. Here is an example of a method that caches the page on the client based on a date. The date should be the exact date of the last time the content changed.

private void SetClientCaching(DateTime lastModified)

{

  Response.Cache.SetETag(lastModified.Ticks.ToString());

  Response.Cache.SetLastModified(lastModified);

  Response.Cache.SetCacheability(HttpCacheability.Public);

  Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));

  Response.Cache.SetSlidingExpiration(true);

}

> >>>

>

>

If you use a HTTP Handler (.ashx) or some other means to serve files – it could be XML-files – it could be a good idea to do a server-side caching as well. Here’s a method you can call that does just that:

private void SetFileCaching(string fileName)

{

  Response.AddFileDependency(fileName);

  Response.Cache.SetETagFromFileDependencies();

  Response.Cache.SetLastModifiedFromFileDependencies();

  Response.Cache.SetCacheability(HttpCacheability.Public);

  Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));

  Response.Cache.SetSlidingExpiration(true);

}

>>

You could use the same method to do standard output caching including client-side caching. >

SetFileCaching(Request.PhysicalPath);

That makes it more powerful than the regular output cache, because it provides client-side caching as well. As mentioned before, there are a lot of ways to use the different kinds of caching and they all bring something to the plate. The code in this example is something I use all the time because it works great for the projects I work on. In the end it is a personal choice.

Among the differences between version 1.1 and 2.0 of .NET Framework is the many new controls. Even though most of them don’t add new functionality they make a lot of things much easier and cleaner than before. Some of these new controls in ASP.NET are the HTML controls HtmlHeadHtmlMeta and HtmlLink.

They don’t add much new functionality by them selves, but look at how clean the code becomes when you use them:

protected void Page_Load(object sender, EventArgs e)

{

  AddMetaContentType();

  AddMetaTag("keywords", "word1, word2, word3...");

  AddMetaTag("description", "bla bla bla");

  AddStyleSheet("/includes/style.css");

}

 

private void AddMetaContentType()

{

  HtmlMeta meta = new HtmlMeta();

  meta.HttpEquiv = "content-type";

  meta.Content = Response.ContentType + "; charset=" + Response.ContentEncoding.HeaderName;

  Page.Header.Controls.Add(meta);

}

 

private void AddMetaTag(string name, string value)

{

  HtmlMeta meta = new HtmlMeta();

  meta.Name = name;

  meta.Content = value;

  Page.Header.Controls.Add(meta);

}

 

private void AddStyleSheet(string relativePath)

{

  HtmlLink link = new HtmlLink();

  link.Href = relativePath;

  link.Attributes["type"] = "text/css";

  link.Attributes["rel"] = "stylesheet";

  Page.Header.Controls.Add(link);

}

This is much more cleaner than adding literal controls to the page header or whatever trick you used before. Just remember to add a runat="server" attribute to the <head> tag of your web page.