Tuesday, August 21, 2012

cache_page directive for ASP.NET MVC

In Ruby on Rails, we should cache the response of the page with cache_page directive on controller.
class SampleController < ApplicationController
  caches_page :home
  caches_action :home_with_filter

  def home

  def home_with_filter

I learned from the book Programming Microsoft® ASP.NET MVC, Second Edition that we should use System.Web.Mvc.OutputCacheAttribute class on each actions like below:
public class SampleController : Controller

    public ActionResult Home()

The advantage of rails way is there are two directives: cache_page and cache_action. cache_page does not execute any actions and filters, but, cache_action only executes filters while ASP.NET MVC has only OutputCache attritbute that does not execute any actions and filters too.

On the other hand, the advantage of ASP.NET MVC way is easy to define expiration seconds of caching while rails' one is more difficult.

No comments:

Post a Comment