public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (filterContext.IsChildAction) { ValidateChildActionConfiguration(); // Already actively being captured? (i.e., cached child action inside of cached child action) // Realistically, this needs write substitution to do properly (including things like authentication) if (GetChildActionFilterFinishCallback(filterContext) != null) { throw new InvalidOperationException(MvcResources.OutputCacheAttribute_CannotNestChildCache); } // Already cached? string uniqueId = GetChildActionUniqueId(filterContext); string cachedValue = ChildActionCacheInternal.Get(uniqueId) as string; if (cachedValue != null) { filterContext.Result = new ContentResult() { Content = cachedValue }; return; } // Swap in a new TextWriter so we can capture the output StringWriter cachingWriter = new StringWriter(CultureInfo.InvariantCulture); TextWriter originalWriter = filterContext.HttpContext.Response.Output; filterContext.HttpContext.Response.Output = cachingWriter; // Set a finish callback to clean up SetChildActionFilterFinishCallback(filterContext, wasException => { // Restore original writer filterContext.HttpContext.Response.Output = originalWriter; // Grab output and write it string capturedText = cachingWriter.ToString(); filterContext.HttpContext.Response.Write(capturedText); // Only cache output if this wasn't an error if (!wasException) { ChildActionCacheInternal.Add(uniqueId, capturedText, DateTimeOffset.UtcNow.AddSeconds(Duration)); } }); } }
public override void OnActionExecuting(ActionExecutingContext filterContext) { //Immediatly return if cache is disabled from admin if (!SettingsHelper.OutputCacheEnabled) { return; } if (filterContext == null) { throw new ArgumentNullException("filterContext"); } var configSection = (OutputCacheSection)ConfigurationManager.GetSection("system.web/caching/outputCache"); if (configSection.EnableOutputCache) { if (filterContext.IsChildAction && SettingsHelper.ChildOutputCacheEnabled) { ValidateChildActionConfiguration(); // Already actively being captured? (i.e., cached child action inside of cached child action) // Realistically, this needs write substitution to do properly (including things like authentication) if (GetChildActionFilterFinishCallback(filterContext) != null) { throw new InvalidOperationException("Cannot nest child cache"); } // Already cached? var uniqueId = GetChildActionUniqueId(filterContext); var cachedValue = ChildActionCacheInternal.Get(uniqueId) as string; if (cachedValue != null) { filterContext.Result = new ContentResult { Content = cachedValue }; return; } // Swap in a new TextWriter so we can capture the output var cachingWriter = new StringWriter(CultureInfo.InvariantCulture); var originalWriter = filterContext.HttpContext.Response.Output; filterContext.HttpContext.Response.Output = cachingWriter; // Set a finish callback to clean up SetChildActionFilterFinishCallback(filterContext, wasException => { // Restore original writer filterContext.HttpContext.Response.Output = originalWriter; // Grab output and write it string capturedText = cachingWriter.ToString(); filterContext.HttpContext.Response.Write(capturedText); // Only cache output if this wasn't an error if (!wasException) { ChildActionCacheInternal.Add(uniqueId, capturedText, DateTimeOffset.UtcNow.AddSeconds(Duration)); } }); } } }