Friday, December 3, 2010

Output caching with ASP.NET MVC Razor

Caching data greatly increase the website performance as its not going to do server round trip. I have already mentioned how you can use Output caching in web forms in earlier blog post here. Let’s see how we can do same thing with mvc. For this example I have used mvc razor. In mvc you can use OutputCache attribute to cache the output. Just like below.

public ActionResult Index()
ViewModel.Message = DateTime.Now.ToString();
return View();
Here it will cache the view for 60 second and will not go for server round trip. Let’s see How it will look into the browser.


You can also set the output caching in web.config and and create output cache profile which you can use any where like following.
varyByParam="none" />
Here how you can use that profile.

[OutputCache(CacheProfile = "MyOuputCacheProfile")]
public ActionResult Index()
ViewModel.Message = DateTime.Now.ToString();
return View();
It support four type of settings for output caching. VaryByContentEncoding, VaryByParam, VaryByCustom,VaryByHeader. Hope this will help you!! happy Programming.

Technorati Tags: ,,
Shout it
kick it on


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My You tube channel- Please Subscribe
Youtube Video Playlist
My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews