5

I am trying to recreate most of the functionality of the OutputCache action filter in MVC 4 by caching view result objects myself. The reason I don't want to use the OutputCache action filter is because I can't use it with AppFabric and partial views; partial views are always stored in MemoryCache and I want the cached objects to be used across a server farm.

The first problem I have is

{"Type 'System.Web.Mvc.TempDataDictionary' cannot be serialized. 
Consider marking it with the DataContractAttribute attribute, and marking all of 
its members you want serialized with the DataMemberAttribute attribute.  
If the type is a collection, consider marking it with the 
CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for  
other supported types."}

This makes me wonder if I should cache something else to return what is essentially the view at the end. Does anyone have an idea of what I should cache instead to recreate the view or a different approach for caching partial views over a server farm? I do not want to use third party plugins for this.

Thanks

Update: I started caching the string representation of the partial view like so:

using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "ViewName");
            ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
            viewResult.View.Render(viewContext, sw);
            view = sw.GetStringBuilder().ToString();
        }

This has made it easy to just retrieve the string in cache and return it as content in the action. I'm still looking for other suggestions or a better way to do this.

Cybermaxs
  • 24,378
  • 8
  • 83
  • 112

1 Answers1

0

Maybe a bit too late but let me share with you my experience.

Whilst ASP.NET MVC is built on top of the ASP.NET framework, it has some quite significant differences that have made the re-use of ASP.NET features rather difficult in MVC. You're true : full page output caching and partial page output caching are implemented in completely different ways. Another blog post from Greg Roberts suggests there are many problems with Output in MVC. It's was so usefull in WebForms !

That's why I turned myself to MvcDonutCaching (Nuget). It has solved many of our problems. Please read the introduction here or on codeplex.

The good news for you is that MvcDonutCaching is also fully compatible with AppFabric Caching ; DevTrends posted an article a few month ago. This means that you can use the new Output Cache Provider (icluded in AppFabric 1.1).

Adding this new provider is fairly simple as adding a reference and changing config in this way.

<caching>
  <outputCache defaultProvider="DistributedCache">
    <providers>
      <add name="DistributedCache"
           type="Microsoft.Web.DistributedCache.DistributedCacheOutputCacheProvider, Microsoft.Web.DistributedCache"
           cacheName="default"
           dataCacheClientName="default" />
    </providers>
  </outputCache>
</caching>

Hope this will help you, because it helps us a lot !

Cybermaxs
  • 24,378
  • 8
  • 83
  • 112