Esempio n. 1
0
        /// <summary>
        /// Execute an 'im mem' preview request of the provided page and content. The execution depends on the hosting environment.
        /// Then running in pipeline mode the current HttpContext is
        /// </summary>
        /// <param name="selectedPage">Page to render. Functionality reading the rendered page ID will get the ID from this object.</param>
        /// <param name="contents">Content to render on the page</param>
        /// <param name="renderingReason">The rendering reason</param>
        /// <returns>The page html as a string when running in classic mode. In Pipeline mode the content is written directly to the HttpContext and an empty string is returned.</returns>
        public static string RenderPreview(IPage selectedPage, IList <IPagePlaceholderContent> contents, RenderingReason renderingReason)
        {
            HttpContext ctx   = HttpContext.Current;
            string      key   = Guid.NewGuid().ToString();
            string      query = "previewKey=" + key;

            ctx.Cache.Add(key + "_SelectedPage", selectedPage, null, Cache.NoAbsoluteExpiration, PreviewExpirationTimeSpan, CacheItemPriority.NotRemovable, null);
            ctx.Cache.Add(key + "_SelectedContents", contents, null, Cache.NoAbsoluteExpiration, PreviewExpirationTimeSpan, CacheItemPriority.NotRemovable, null);
            ctx.Cache.Add(key + "_RenderingReason", renderingReason, null, Cache.NoAbsoluteExpiration, PreviewExpirationTimeSpan, CacheItemPriority.NotRemovable, null);

            if (HttpRuntime.UsingIntegratedPipeline)
            {
                // The header trick here is to work around (what seems to be) a bug in .net 4.5, where preserveForm=false is ignored
                // asp.net 4.5 request validation will see the 'page edit http post' data and start bitching. It really should not.
                var headers = new System.Collections.Specialized.NameValueCollection();
                headers.Add("Content-Length", "0");

                string cookieHeader = ctx.Request.Headers["Cookie"];
                if (!string.IsNullOrEmpty(cookieHeader))
                {
                    headers.Add("Cookie", cookieHeader);
                }

                ctx.Server.TransferRequest("~/Renderers/Page.aspx?" + query, false, "GET", headers);
            }
            else
            {
                var sb     = new StringBuilder();
                var writer = new StringWriter(sb);

                var wr = new PreviewWorkerRequest(query, ctx, writer);

                AllowChildRequestSessionAccess(ctx);
                HttpRuntime.ProcessRequest(wr);

                // Preview <iframe /> has source "~/Composite/content/flow/FlowUi.aspx" and page is rendered from "~/Renderers/Page.aspx"
                // The following line fixes style references processed by ASP.NET-s Control.ResolveClientUrl()
                sb = sb.Replace("href=\"../", "href=\"../../../");

                return(sb.ToString());
            }

            return(String.Empty);
        }
Esempio n. 2
0
        /// <summary>
        /// Execute an 'im mem' preview request of the provided page and content. The execution depends on the hosting environment.
        /// Then running in pipeline mode the current HttpContext is 
        /// </summary>
        /// <param name="selectedPage">Page to render. Functionality reading the rendered page ID will get the ID from this object.</param>
        /// <param name="contents">Content to render on the page</param>
        /// <param name="renderingReason">The rendering reason</param>
        /// <returns>The page html as a string when running in classic mode. In Pipeline mode the content is written directly to the HttpContext and an empty string is returned.</returns>
        public static string RenderPreview(IPage selectedPage, IList<IPagePlaceholderContent> contents, RenderingReason renderingReason)
        {
            HttpContext ctx = HttpContext.Current;
            string key = Guid.NewGuid().ToString();
            string query = "previewKey=" + key;

            ctx.Cache.Add(key + "_SelectedPage", selectedPage, null, Cache.NoAbsoluteExpiration, PreviewExpirationTimeSpan, CacheItemPriority.NotRemovable, null);
            ctx.Cache.Add(key + "_SelectedContents", contents, null, Cache.NoAbsoluteExpiration, PreviewExpirationTimeSpan, CacheItemPriority.NotRemovable, null);
            ctx.Cache.Add(key + "_RenderingReason", renderingReason, null, Cache.NoAbsoluteExpiration, PreviewExpirationTimeSpan, CacheItemPriority.NotRemovable, null);

            if (HttpRuntime.UsingIntegratedPipeline)
            {
                // The header trick here is to work around (what seems to be) a bug in .net 4.5, where preserveForm=false is ignored
                // asp.net 4.5 request validation will see the 'page edit http post' data and start bitching. It really should not.
                var headers = new System.Collections.Specialized.NameValueCollection();
                headers.Add("Content-Length", "0");

                string cookieHeader = ctx.Request.Headers["Cookie"];
                if (!string.IsNullOrEmpty(cookieHeader))
                {
                    headers.Add("Cookie", cookieHeader);
                }

                ctx.Server.TransferRequest("~/Renderers/Page.aspx?" + query, false, "GET", headers);
            }
            else
            {
                var sb = new StringBuilder();
                var writer = new StringWriter(sb);

                var wr = new PreviewWorkerRequest(query, ctx, writer);

                AllowChildRequestSessionAccess(ctx);
                HttpRuntime.ProcessRequest(wr);

                // Preview <iframe /> has source "~/Composite/content/flow/FlowUi.aspx" and page is rendered from "~/Renderers/Page.aspx"
                // The following line fixes style references processed by ASP.NET-s Control.ResolveClientUrl()
                sb = sb.Replace("href=\"../", "href=\"../../../");

                return sb.ToString();
            }

            return String.Empty;
        }