Thanks.
TimeSpan freshness = new TimeSpan(0, 0, 5, 0);
DateTime now = DateTime.Now;
context.Response.Cache.SetExpires(now.Add(freshness));
context.Response.Cache.SetMaxAge(freshness);
context.Response.Cache.SetCacheability(HttpCacheability.Server);
context.Response.Cache.SetValidUntilExpires(true);
You can test it out pretty easily – write a handler which returns the current time
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World " + DateTime.Now.ToLongTimeString());
And run the handler without the caching code a few times. The seconds will update (as you would expect). Add that block of code above and run it again, voila – the time won’t change, its serving the page out of the output cache now.
It supports all the same methods the declarative approach does and affords you even more control.
context.Response.Cache.VaryByParams["C"] = true;
-Cramer
<John Smith> wrote in message news:200859191136e...@gmail.com...
> Cramer,
> You can utilize output caching on a handler, however you have to do it via
> code instead of declaratively. If you add this block of code inside the
> "ProcessRequest" method your handler response will be cached.
>
> TimeSpan freshness = new TimeSpan(0, 0, 5, 0);
> DateTime now = DateTime.Now;
> context.Response.Cache.SetExpires(now.Add(freshness));
> context.Response.Cache.SetMaxAge(freshness);
> context.Response.Cache.SetCacheability(HttpCacheability.Server);
> context.Response.Cache.SetValidUntilExpires(true);
>
> You can test it out pretty easily - write a handler which returns the
> current time
>
> context.Response.ContentType = "text/plain";
> context.Response.Write("Hello World " + DateTime.Now.ToLongTimeString());
>
> And run the handler without the caching code a few times. The seconds will
> update (as you would expect). Add that block of code above and run it
> again, voila - the time won't change, its serving the page out of the