public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues)
 {
     using (var writer = new StringBlockWriter(CultureInfo.CurrentCulture))
     {
         ActionHelper(htmlHelper, actionName, controllerName, routeValues, writer);
         return(MvcHtmlString.Create(writer.ToString()));
     }
 }
Example #2
0
 public static MvcHtmlString Partial(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData)
 {
     using (var writer = new StringBlockWriter(CultureInfo.CurrentCulture))
     {
         htmlHelper.RenderPartialInternal(partialViewName, viewData, model, writer, ViewEngines.Engines);
         return(MvcHtmlString.Create(writer.ToString()));
     }
 }
        public static async Task <MvcHtmlString> ActionAsync(this HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues)
        {
            using (var writer = new StringBlockWriter(CultureInfo.CurrentCulture))
            {
                await ActionHelperAsync(htmlHelper, actionName, controllerName, routeValues, writer).ConfigureAwait(false);

                return(MvcHtmlString.Create(writer.ToString()));
            }
        }
Example #4
0
        public static async Task <MvcHtmlString> PartialAsync(this HtmlHelper htmlHelper, string partialViewName, object model, ViewDataDictionary viewData)
        {
            using (var writer = new StringBlockWriter(CultureInfo.CurrentCulture))
            {
                await htmlHelper.RenderPartialInternalAsync(partialViewName, viewData, model, writer, ViewEngines.Engines).ConfigureAwait(false);

                return(MvcHtmlString.Create(writer.ToString()));
            }
        }
Example #5
0
            public override string Execute(HtmlHelper html, ViewDataDictionary viewData)
            {
                ViewEngineResult viewEngineResult = ViewEngines.Engines.FindPartialView(html.ViewContext, ViewName);

                using (var writer = new StringBlockWriter(CultureInfo.InvariantCulture))
                {
                    viewEngineResult.View.Render(new ViewContext(html.ViewContext, viewEngineResult.View, viewData, html.ViewContext.TempData, writer), writer);
                    return(writer.ToString());
                }
            }
Example #6
0
        internal static string ExecuteTemplate(HtmlHelper html, ViewDataDictionary viewData, string templateName, DataBoundControlMode mode, GetViewNamesDelegate getViewNames, GetDefaultActionsDelegate getDefaultActions)
        {
            Dictionary <string, ActionCacheItem>            actionCache    = GetActionCache(html);
            Dictionary <string, Func <HtmlHelper, string> > defaultActions = getDefaultActions(mode);
            string modeViewPath = _modeViewPaths[mode];

            foreach (string viewName in getViewNames(viewData.ModelMetadata, templateName, viewData.ModelMetadata.TemplateHint, viewData.ModelMetadata.DataTypeName))
            {
                string          fullViewName = modeViewPath + "/" + viewName;
                ActionCacheItem cacheItem;

                if (actionCache.TryGetValue(fullViewName, out cacheItem))
                {
                    if (cacheItem != null)
                    {
                        return(cacheItem.Execute(html, viewData));
                    }
                }
                else
                {
                    ViewEngineResult viewEngineResult = ViewEngines.Engines.FindPartialView(html.ViewContext, fullViewName);
                    if (viewEngineResult.View != null)
                    {
                        actionCache[fullViewName] = new ActionCacheViewItem {
                            ViewName = fullViewName
                        };

                        using (var writer = new StringBlockWriter(CultureInfo.InvariantCulture))
                        {
                            viewEngineResult.View.Render(new ViewContext(html.ViewContext, viewEngineResult.View, viewData, html.ViewContext.TempData, writer), writer);
                            return(writer.ToString());
                        }
                    }

                    Func <HtmlHelper, string> defaultAction;
                    if (defaultActions.TryGetValue(viewName, out defaultAction))
                    {
                        actionCache[fullViewName] = new ActionCacheCodeItem {
                            Action = defaultAction
                        };
                        return(defaultAction(MakeHtmlHelper(html, viewData)));
                    }

                    actionCache[fullViewName] = null;
                }
            }

            throw new InvalidOperationException(
                      String.Format(
                          CultureInfo.CurrentCulture,
                          MvcResources.TemplateHelpers_NoTemplate,
                          viewData.ModelMetadata.RealModelType.FullName));
        }
Example #7
0
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            if (filterContext.IsChildAction)
            {
                // Skip validation and caching if there's no caching on the server for this action. It's ok to
                // explicitly disable caching for a child action, but it will be ignored if the parent action
                // is using caching.
                if (IsServerSideCacheDisabled())
                {
                    return;
                }

                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
                var        cachingWriter  = new StringBlockWriter(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));
                    }
                });
            }
        }