ASP.NET Secrets, Part 4
Welcome to part four of ASP.NET Secrets!
I'm Karl Moore and once again, we're going wild with Web application tricks. Adapted from the full-length versions in my new book, VB.NET and ASP.NET Secrets, here's what we'll be covering today:
- Create Super Fast ASP.NET Applications, with Caching
- Hiding Error Code from Your Clients
- Forget 404: Customizing Your Page Not Found
- Ten Steps to Successful Debugging
Don't forget that you can contribute to this series, yourself! Simply mail me—email@example.com—with your favourite .NET tips and tricks. I'll publish the best, with full credit given.
But enough chatting. Ready to get started? Let's go!
Create Super Fast ASP.NET Applications, with Caching
Some pages on your Web site will be awfully "expensive" to generate. By this, I mean they take time for your server to put together. Perhaps your page grabs the latest stock figures from an external site, perhaps it performs a long company calculation, perhaps it simply delves into your database and retrieves a news story.
Either way, it all takes expensive processor time, and it would be seriously useful if you could just "cache" those pages once they've been generated and automatically serve that same page back up for a certain period of time. When the period is up, the page can then run its code once again and "refresh."
Sounds like a great method of optimizing your site—but perhaps not completely easy to implement. Well, actually, it is. In fact, to set the most popular form of ASP.NET caching into action, you'll need only one line of code. Here's how...
If you want to cache your page for, say, ten minutes, first open your .ASPX page in Design mode. Next, click the HTML button (or from the menu, select View, HTML Source). Just above the <%@ Page Language="vb" ... > directive, add the following line of code:
<%@ OutputCache Duration="600" VaryByParam="None" %>
This tells ASP.NET to cache the page after its first request for ten minutes (600 seconds). You can test caching by creating a page that simply displays the time as it loads, whilst incorporating an OutputCache directive with a short Duration attribute. Then, keep refreshing the page in your browser; only after the set Duration will the time update itself.
You may be wondering what the VaryByParam attribute is all about. Well, it tells ASP.NET whether it should cache all requests for this page, or cache separate versions, depending on whether the query string changes (in other words, the "parameters" to the page change).
For example, if you want two versions of a page cached when someone requests "article.aspx?id=10" and "article.aspx?id=11", then you'll want to change your OutputCache directive to:
<%@ OutputCache Duration="600" VaryByParam="id" %>
Alternatively, if you want a version of the page cached whenever any parameter alters, simply use a wildcard, as so:
<%@ OutputCache Duration="600" VaryByParam="*" %>
Here, no matter whether you request "article.aspx?id=12" or "article.aspx?tag=loveshack", a different version of the page will be cached and re-served whenever requested again within the specified duration.
If you're interested in caching just portions of your page, it's interesting to note that you also can use this technique in user controls (see the "The Secret Behind User Controls" tip earlier in this chapter). This could perhaps allow you to have certain parts of the page updated with every request, whilst other, more common elements, simply refresh every hour or so.
A quick word of warning: Be careful not to overuse caching. Used wisely, it can greatly increase the performance of your Web applications. Used in a crazed, insane fashion—say, on a free text search page—it'll bring your server to a grinding halt.
You can learn more about ASP.NET caching by looking up 'ASP.NET caching' in the Help Index. Surprising, that.