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)); }
internal bool LoadRazorTemplate( string virtualPath, out WebPageBase webPage, out PageTemplateDescriptor parsedTemplate, out IDictionary <string, PropertyInfo> placeholderProperties, out Exception loadingException) { try { webPage = WebPageBase.CreateInstanceFromVirtualPath(virtualPath); } catch (Exception ex) { Log.LogError(LogTitle, "Failed to compile razor file '{0}'", virtualPath); Log.LogError(LogTitle, ex); loadingException = ex is TargetInvocationException ? ex.InnerException : ex; webPage = null; parsedTemplate = null; placeholderProperties = null; return(false); } if (webPage == null || !(webPage is RazorPageTemplate)) { parsedTemplate = null; placeholderProperties = null; loadingException = null; return(true); } RazorPageTemplate razorPageTemplate = webPage as RazorPageTemplate; razorPageTemplate.Configure(); try { ParseTemplate(virtualPath, razorPageTemplate, out parsedTemplate, out placeholderProperties); } catch (Exception ex) { Log.LogError(LogTitle, "Failed to load razor page template '{0}'", virtualPath); Log.LogError(LogTitle, ex); loadingException = ex; parsedTemplate = null; placeholderProperties = null; return(false); } finally { razorPageTemplate.Dispose(); } loadingException = null; return(true); }
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); } }