private async Task <OutputCacheModel> GenerateViewAsString(FilterContext context, ViewResult viewResult) { string viewName = viewResult.ViewName != null ? viewResult.ViewName : context.RouteData.Values["action"].ToString(); IViewEngine viewEngine = context.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine; ViewEngineResult viewEngineResult = viewEngine.FindView(context, viewName, true); using (var writer = new StringWriter()) { if (viewEngineResult.Success) { ViewContext viewContext = new ViewContext(context, viewEngineResult.View, viewResult.ViewData, viewResult.TempData, writer, new HtmlHelperOptions()); await viewEngineResult.View.RenderAsync(viewContext); string content = writer.GetStringBuilder().ToString(); var outputCache = new OutputCacheModel() { Content = content, ContentType = context.HttpContext.Response.ContentType, StatusCode = context.HttpContext.Response.StatusCode }; return(outputCache); } else { throw new FileNotFoundException($"A view with the name {viewName} could not be found"); } } }
public IActionResult Index() { OutputCacheModel OutputCache = new OutputCacheModel() { Content = "Test Content" }; return(View(OutputCache)); }
public override async void OnResultExecuted(ResultExecutedContext context) { string cacheKey = $"{context.RouteData.Values["controller"].ToString()}-{context.RouteData.Values["action"].ToString()}"; Console.WriteLine("Duration is " + Duration); if (cache.Get(cacheKey) == null) { Console.WriteLine("Cache null"); if (context.Result is ObjectResult objectResult) { string value = context.HttpContext.Response.ContentType.StartsWith("application/json") ? JsonConvert.SerializeObject(objectResult.Value, jsonSettings) : objectResult.Value.ToString(); var outputCache = new OutputCacheModel() { Content = value, ContentType = context.HttpContext.Response.ContentType, StatusCode = context.HttpContext.Response.StatusCode }; cache.Set(cacheKey, outputCache, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(Duration))); } else if (context.Result is ViewResult viewResult) { Console.WriteLine($"\n\nActionResult is: {viewResult}\n\n"); string viewName = viewResult.ViewName != null ? viewResult.ViewName : context.RouteData.Values["action"].ToString(); using (var writer = new StringWriter()) { IViewEngine viewEngine = context.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine; ViewEngineResult viewEngineResult = viewEngine.FindView(context, viewName, true); if (viewEngineResult.Success) { ViewContext viewContext = new ViewContext(context, viewEngineResult.View, viewResult.ViewData, viewResult.TempData, writer, new HtmlHelperOptions()); await viewEngineResult.View.RenderAsync(viewContext); string content = writer.GetStringBuilder().ToString(); var outputCache = new OutputCacheModel() { Content = content, ContentType = context.HttpContext.Response.ContentType, StatusCode = context.HttpContext.Response.StatusCode }; cache.Set(cacheKey, outputCache, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(Duration))); Console.WriteLine("Result is : "); } else { Console.WriteLine($"A view with the name {viewName} could not be found"); } } } } }
public async void OnResourceExecuted(ResourceExecutedContext context) { string cacheKey = $"{context.RouteData.Values["controller"].ToString()}-{context.RouteData.Values["action"].ToString()}"; if (cache.Get(cacheKey) == null) { OutputCacheModel outputCache = null; if (context.Result is ObjectResult objectResult) { // Check if objectResult is JSON string value = context.HttpContext.Response.ContentType.StartsWith("application/json") ? JsonConvert.SerializeObject(objectResult.Value, jsonSettings) : objectResult.Value.ToString(); outputCache = new OutputCacheModel() { Content = value, ContentType = context.HttpContext.Response.ContentType, StatusCode = context.HttpContext.Response.StatusCode }; } else if (context.Result is JsonResult jsonResult) { outputCache = new OutputCacheModel() { Content = JsonConvert.SerializeObject(jsonResult.Value, jsonSettings), ContentType = context.HttpContext.Response.ContentType, StatusCode = context.HttpContext.Response.StatusCode }; } else if (context.Result is ViewResult viewResult) { outputCache = await GenerateViewAsString(context, viewResult); } if (outputCache != null) { cache.Set(cacheKey, outputCache, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(Duration))); } } }