Output caching with ASP.NET MVC Razor

No.of Views1610
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  05 Dec 2010 20:12:56
Tag : ASP.NET , ASP.NET MVC
Caching data greatly increase the website performance as its not going to do server round trip.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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 article here. Let’s see how we can do same thing with asp.net mvc. For this example I have used asp.net mvc razor. In asp.net mvc you can use OutputCache attribute to cache the output.

Just like below.

[OutputCache(VaryByParam="none",Duration=60)]
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. 

Image Loading

You can also set the output caching in web.config and and create output cache profile which you can use any where like following.

<caching>
<outputCacheSettings>
  <outputCacheProfiles>
    <clear/>
    <add
      name="MyOuputCacheProfile"
      duration="60"
      varyByParam="none" />
  </outputCacheProfiles>
</outputCacheSettings>
</caching>

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.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Gold
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top