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.