Пример #1
0
        /// <summary>
        /// Renders/executes the provided macro ActionResult.
        /// </summary>
        /// <remarks>Macro rendering time is traced.</remarks>
        private static string GetMacroStringOutput(string macroAlias, ActionResult ar, ControllerContext currentControllerContext)
        {
            using (DisposableTimer.Start(timer =>
                            LogHelper.TraceIfEnabled<MacroRenderer>("GetMacroStringOutput for {0} took {1}ms", () => macroAlias, () => timer)))
            {
                if (ar is ViewResultBase)
                {
                    var viewResult = (ViewResultBase)ar;

                    return currentControllerContext.RenderViewResultAsString(viewResult);
                }
                if (ar is ContentResult)
                {
                    var contentResult = (ContentResult)ar;
                    return contentResult.Content;
                }
                throw new NotSupportedException("Its not possible to retreive the output of a macro that doesn't return a ViewResultBase");
            }
        }
Пример #2
0
        /// <summary>
        /// Renders the macro output as a string
        /// </summary>
        /// <param name="macroAlias"></param>
        /// <param name="macroParams"></param>
        /// <param name="currentControllerContext"></param>
        /// <param name="isForRichTextEditor">If the request is to render the contents in the back office rich text editor</param>
        /// <param name="resolveContent"></param>
        /// <returns></returns>
        public string RenderMacroAsString(
            string macroAlias,
            IDictionary<string, string> macroParams,
            ControllerContext currentControllerContext,
            bool isForRichTextEditor,
            Func<Content> resolveContent)
        {
            //method to get the string output of the ActionResult
            Func<ActionResult, string> getStringOutput = ar =>
                {
                    if (ar is ViewResultBase)
                    {
                        var viewResult = (ViewResultBase)ar;

                        return currentControllerContext.RenderViewResultAsString(viewResult);
                    }
                    if (ar is ContentResult)
                    {
                        var contentResult = (ContentResult)ar;
                        return contentResult.Content;
                    }
                    throw new NotSupportedException("Its not possible to retreive the output of a macro that doesn't return a ViewResultBase");
                };

            if (isForRichTextEditor)
            {
                return getStringOutput(RenderMacro(macroAlias, macroParams, currentControllerContext, true, resolveContent));
            }

            //if we not rendering for the rich text editor then check if we've cached this in the ScopedCache first, 
            //even though we are caching some macros in application cache, we're only caching the ActionResult and not the  text
            //output which still requires a tiny amount of processing, so we'll store all equal macro string output in ScopedCache too, 
            //as this will speed things regardless of whehter macros are cached or not if there's a few of the same ones per page

            return _applicationContext.FrameworkContext
                .ScopedCache.GetOrCreate("umb-macro-" + macroAlias + macroParams.GetHashCode(),
                                         () => getStringOutput(RenderMacro(macroAlias, macroParams, currentControllerContext, false, resolveContent)))
                                         .ToString();
        }