Announcing the ASP.NET MVC 3 Release Candidate
page 5 of 13
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 20162/ 84

Partial Page Output Caching

ASP.NET MVC has supported output caching of full page responses since V1.  With ASP.NET MVC V3 (starting with today’s RC) we are also enabling support for partial page output caching – which allows you to easily output cache regions or fragments of a response as opposed to the entire thing.  This ends up being super useful in a lot of scenarios.

Output caching just a region of a page is really easy to-do.  Simply encapsulate the region you want to output cache within a child action that you invoke from within the view that you are rendering.  For example, below I have a product listing page, and I want to output a “Daily Specials” section on the page as well:

Above I’m using the Html.Action() helper method to call the SalesController.DailySpecials() child action method.  Notice that I’m passing a category parameter to it above – which will allow me to customize the “Daily Specials” I display based on what types of products the user is currently browsing (that way if they are browsing “computer” products I can display a list of computer specials, and if they are browsing “baby” products I can display diaper specials).

Below is a simple implementation of the SalesController.DailySpecials() method.  It retrieves an appropriate list of products and then renders back a response using a Razor partial view template:

image

Notice how the DailySpecials method above has an [OutputCache] attribute on it.  This indicates that the partial content rendered by it should be cached (for 3600 seconds/1 hour).  We are also indicating that the cached content should automatically vary based on the category parameter.

If we have 10 categories of products, our DailySpecials method will end up caching 10 different lists of specials – and the appropriate specials list (computers or diapers) will be output depending upon what product category the user is browsing in.  Importantly: no database access or processing logic will happen if the partial content is served out of the output cache – which will reduce the load on our server and speed up the response time.

This new mechanism provides a pretty clean and easy way to add partial-page output caching to your applications.


View Entire Article

User Comments

Title: Help with output catching   
Name: Victor D
Date: 12/2/2010 6:53:47 PM
Comment:
Hello, I'm implementing the output catching but I need to pass a parameter and with the code in this page (passing a category parameter) didn't work. Always render the same page even if I change the parameter value
Title: MVC3   
Name: Ansal
Date: 11/25/2010 12:02:11 AM
Comment:
useful One

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 8/22/2014 7:34:10 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search