开发者

Nested partial output caching in ASP.NET MVC 3

I am using Razor view engine in ASP.Net MVC 3 RC 2. This is part of my view city.cshtml

(drastically simplified code for the sake of simplicity in example)

<!-- in city.cshtml -->
<div class="list">
@foreach(var product in SQL.GetProducts(Model.City) )
{
  <div class="product">
    <div>@product.Name</div>
    <div class="category">
    @foreach(var category in SQL.GetCategories(product.ID) )
    {
      &开发者_Go百科lt;a href="@category.Url">@category.Name</a> » 
    }
    </div>
  </div>
}
</div>

I want to cache this part of my output using OutputCache attribute, so I created an action ProductList with OutputCache attribute enabled

<!-- in city.cshtml -->
<div class="list">
  @Html.Action("ProductList", new { City = Model.City })
</div>

and I created the view in ProductList.cshtml as below

<!-- in ProductList.cshtml -->
@foreach(var product in Model.Products )
{
  <div class="product">
    <div>@product.Name</div>
    <div class="category">
    @foreach(var category in SQL.GetCategories(product.ID) ) 
    {
      <a href="@category.Url">@category.Name</a> » 
    }
    </div>
  </div>
}

but I still need to cache the category path output on each product. so I created an action CategoryPath with OutputCache attribute enabled

<!-- in ProductList.cshtml -->
@foreach(var product in Model.Products ){
  <div class="product">
    <div>@product.Name</div>
    <div class="category">
      @Html.Action("CategoryPath", new { ProductID = product.ID })
    </div>
  </div>
}

But apparently this is not allowed. I got this error:

OutputCacheAttribute is not allowed on child actions which are children of an already cached child action.

I believe they have a good reason why they need to disallow this. I really want this kind of nested Output Caching.

Any idea for a workaround?


We had a similar issue, and found that we didn't actually NEED nested partial output caching at all. Once we were caching the parent object, the child was not called again until the parent's cache expired.

So, my advice might be: Cache the parent and the child will already be handled.


Use Child Action Cache For CategoryPath Action. There's also an example of ChildActionOnly in action.


In you SQL.GetCategories method you can get all categories and cache it if not already in cache. And filter categories by productID using LINQ TO OBJECTS. In this way you do not hit db every time you need to find categories of a product.

Now you only use OutputCache on ProductList, and you have a pretty decent performing (partial)view.


I don't know much about MVC, but "nested caching" made me wonder why using varybyparam won't work here...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜