ASP.NET MVC > Caching

Cache output of action method in ASP.NET MVC

How to cache the output of the action method?


To cache (temporary store the application specific data to send to the user) the output of the action method, we can use OutputCache attribute in the action method of the controller.

CONTROLLER METHOD

  [OutputCache(Duration=10)]
  public string Index()
  {
      return DateTime.Now.ToString();
  }

The output of this action method will be cached for 10 seconds (from the first request). Calling this action method simply show the current date in the browser in all further request till next 10 seconds and after 10 seconds, the fresh date and time is shown  in the browser and the same is cached again for 10 seconds and the same follows for next requests. OutputCache attribute can be applied to any action method that returns ActionResult.

 Views: 14734 | Post Order: 96



Write for us






Hosting Recommendations