public void AttachToPage(Page aspnetPage, PageContentToRender contentToRender) { Verify.ArgumentNotNull(aspnetPage, "aspnetPage"); Verify.ArgumentNotNull(contentToRender, "contentToRender"); aspnetPage.Items.Add(PageRenderingJob_Key, contentToRender); Guid templateId = contentToRender.Page.TemplateId; var rendering = _renderingInfo[templateId]; if (rendering == null) { Exception loadingException = _loadingExceptions[templateId]; if (loadingException != null) { throw loadingException; } Verify.ThrowInvalidOperationException("Failed to get master page by template ID '{0}'. Check for compilation errors".FormatWith(templateId)); } aspnetPage.MasterPageFile = rendering.VirtualPath; aspnetPage.PreRender += (e, args) => PageOnPreRender(aspnetPage, contentToRender.Page); var master = aspnetPage.Master as MasterPagePageTemplate; TemplateDefinitionHelper.BindPlaceholders(master, contentToRender, rendering.PlaceholderProperties, null); }
public XDocument Render(PageContentToRender contentToRender, FunctionContextContainer functionContextContainer) { Guid templateId = contentToRender.Page.TemplateId; var renderingInfo = _renderingInfo[templateId]; if (renderingInfo == null) { Exception loadingException = _loadingExceptions[templateId]; if (loadingException != null) { throw loadingException; } Verify.ThrowInvalidOperationException($"Missing template '{templateId}'"); } string output; RazorPageTemplate webPage = null; try { webPage = WebPageBase.CreateInstanceFromVirtualPath(renderingInfo.ControlVirtualPath) as RazorPageTemplate; Verify.IsNotNull(webPage, "Razor compilation failed or base type does not inherit '{0}'", typeof(RazorPageTemplate).FullName); webPage.Configure(); using (Profiler.Measure("Evaluating placeholders")) { TemplateDefinitionHelper.BindPlaceholders(webPage, contentToRender, renderingInfo.PlaceholderProperties, functionContextContainer); } // Executing razor code var httpContext = new HttpContextWrapper(HttpContext.Current); var startPage = StartPage.GetStartPage(webPage, "_PageStart", new[] { "cshtml" }); var pageContext = new WebPageContext(httpContext, webPage, startPage); pageContext.PageData.Add(RazorHelper.PageContext_FunctionContextContainer, functionContextContainer); var sb = new StringBuilder(); using (var writer = new StringWriter(sb)) { using (Profiler.Measure("Executing Razor page template")) { webPage.ExecutePageHierarchy(pageContext, writer); } } output = sb.ToString(); } finally { webPage?.Dispose(); } return(XDocument.Parse(output)); }
private void RendererPage(object sender, EventArgs e) { Guid templateId = _job.Page.TemplateId; var renderingInfo = _renderingInfo[templateId]; if (renderingInfo == null) { Exception loadingException = _loadingExceptions[templateId]; if (loadingException != null) { throw loadingException; } Verify.ThrowInvalidOperationException("Missing template '{0}'".FormatWith(templateId)); } string output; FunctionContextContainer functionContextContainer; RazorPageTemplate webPage = null; try { webPage = WebPageBase.CreateInstanceFromVirtualPath(renderingInfo.ControlVirtualPath) as AspNet.Razor.RazorPageTemplate; Verify.IsNotNull(webPage, "Razor compilation failed or base type does not inherit '{0}'", typeof(AspNet.Razor.RazorPageTemplate).FullName); webPage.Configure(); functionContextContainer = PageRenderer.GetPageRenderFunctionContextContainer(); using (Profiler.Measure("Evaluating placeholders")) { TemplateDefinitionHelper.BindPlaceholders(webPage, _job, renderingInfo.PlaceholderProperties, functionContextContainer); } // Executing razor code var httpContext = new HttpContextWrapper(HttpContext.Current); var startPage = StartPage.GetStartPage(webPage, "_PageStart", new[] { "cshtml" }); var pageContext = new WebPageContext(httpContext, webPage, startPage); pageContext.PageData.Add(RazorHelper.PageContext_FunctionContextContainer, functionContextContainer); var sb = new StringBuilder(); using (var writer = new StringWriter(sb)) { using (Profiler.Measure("Executing Razor page template")) { webPage.ExecutePageHierarchy(pageContext, writer); } } output = sb.ToString(); } finally { if (webPage != null) { webPage.Dispose(); } } XDocument resultDocument = XDocument.Parse(output); var controlMapper = (IXElementToControlMapper)functionContextContainer.XEmbedableMapper; Control control = PageRenderer.Render(resultDocument, functionContextContainer, controlMapper, _job.Page); using (Profiler.Measure("ASP.NET controls: PagePreInit")) { _aspnetPage.Controls.Add(control); } }