Here's a quick tip for something pretty neat in ASP.NET 2.0. You can specify output cache profiles in the application configuration like so (this is under the system.web configuration section):
Then in your Global.asax code, add the following handler:
Now, on any pages you want to be cached like this, you can just add the output cache directive like so:
Of course, you can add other profiles for pages that, say, vary by parameters, controls, and the like, but this makes it easy to control the caching of all of a kind of a page via the web.config and also shows how you might set up a profile that varies by browser and culture. And with an implementation of GetVaryByCustom like this, you can add your own variances and mix and match them in your profiles as desired. It makes for a pretty flexible caching system. For instance, you could add a check for authentication and, if authenticated, effectively turn off caching by appending a user ID or name and the current date and time (DateTime.Now.ToString()). That way pages would be cached for anonymous visitors but for authenticated users, where the content is more dynamic, you might not want to cache.
The opinions expressed herein are solely my own personal opinions, founded or unfounded, rational or not, and you can quote me on that.
Thanks to the good folks at dasBlog!
Copyright © 2017 J. Ambrose Little