private static void RenderComment(HttpContext context, Comment comment) { var page = (WebPage)WebPageBase.CreateInstanceFromVirtualPath("~/themes/" + Blog.Theme + "/comment.cshtml"); page.Context = new HttpContextWrapper(context); page.ExecutePageHierarchy(new WebPageContext(page.Context, page: null, model: comment), context.Response.Output); }
private string RenderRazorFile <T>(string templateFile, int?pageId, T model) { string rtnStr; WebPageBase razorWebPage = WebPageBase.CreateInstanceFromVirtualPath(templateFile); razorWebPage.Context = new HttpContextWrapper(HttpContext.Current); if (razorWebPage is IMacroContext) { if (razorWebPage is TemplateContext <T> && model != null) { ((TemplateContext <T>)razorWebPage).SetMembers(model); } else if (pageId != null) { ((IMacroContext)razorWebPage).SetMembers(new MacroModel(), new Node(pageId.Value)); } } using (StringWriter output = new StringWriter()) { razorWebPage.ExecutePageHierarchy(new WebPageContext(razorWebPage.Context, razorWebPage, null), output); rtnStr = output.ToString(); } return(rtnStr); }
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); }
public static WebPage CompileAndInstantiate(string virtualPath) { //Compile Razor - We Will Leave This To ASP.NET Compilation Engine & ASP.NET WebPages //Security in medium trust is strict around here, so we can only pass a virtual file path //ASP.NET Compilation Engine caches returned types //Changed From BuildManager As Other Properties Are Attached Like Context Path/ var webPageBase = WebPageBase.CreateInstanceFromVirtualPath(virtualPath); var webPage = webPageBase as WebPage; if (webPage == null) { throw new InvalidCastException("Context Must Implement System.Web.WebPages.WebPage"); } return(webPage); }
private static PartialViewMacroPage CompileAndInstantiate(string virtualPath) { //Compile Razor - We Will Leave This To ASP.NET Compilation Engine & ASP.NET WebPages //Security in medium trust is strict around here, so we can only pass a virtual file path //ASP.NET Compilation Engine caches returned types //Changed From BuildManager As Other Properties Are Attached Like Context Path/ var webPageBase = WebPageBase.CreateInstanceFromVirtualPath(virtualPath); var webPage = webPageBase as PartialViewMacroPage; if (webPage == null) { throw new InvalidCastException("All Partial View Macro views must inherit from " + typeof(PartialViewMacroPage).FullName); } return(webPage); }
public WebPageHttpHandler(string virtualPath) { // create a new WebPage instance for use when processing the request this.Page = (WebPage)WebPageBase.CreateInstanceFromVirtualPath(virtualPath); Func <WebPageRenderingBase> func = null; if (func == null) { func = delegate { return(StartPage.GetStartPage(this.Page, "_PageStart", System.Web.WebPages.WebPageHttpHandler.GetRegisteredExtensions())); }; } this._startPage = new Lazy <WebPageRenderingBase>(func); }
private StringWriter RenderTemplate(string virtualPath, dynamic model) { var page = WebPageBase.CreateInstanceFromVirtualPath(virtualPath); var httpContext = new HttpContextWrapper(HttpContext.Current); var pageContext = new WebPageContext(httpContext, page, model); var writer = new StringWriter(); if (page is WebPage) { page.ExecutePageHierarchy(pageContext, writer); } else { var razorEngine = new RazorEngine(virtualPath, null, null); razorEngine.Render <dynamic>(writer, model); } return(writer); }
private static string RenderTemplate(string virtualPath, dynamic model) { var page = WebPageBase.CreateInstanceFromVirtualPath(virtualPath); var httpContext = new HttpContextWrapper(HttpContext.Current); var pageContext = new WebPageContext(httpContext, page, model); using (var writer = new StringWriter()) { if (page is WebPage) { page.ExecutePageHierarchy(pageContext, writer); } else { throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "The webpage at '{0}' must inherit from System.Web.WebPages.WebPage", virtualPath)); } return(writer.ToString()); } }
/// <summary> /// Executes the razor page. /// </summary> /// <param name="virtualPath">The virtual path.</param> /// <param name="setParameters">Delegate to set the parameters.</param> /// <param name="resultType">The type of the result.</param> /// <param name="functionContextContainer">The function context container</param> /// <returns></returns> public static object ExecuteRazorPage( string virtualPath, Action<WebPageBase> setParameters, Type resultType, FunctionContextContainer functionContextContainer) { WebPageBase webPage = null; try { webPage = WebPageBase.CreateInstanceFromVirtualPath(virtualPath); return ExecuteRazorPage(webPage, setParameters, resultType, functionContextContainer); } finally { if (webPage is IDisposable) { (webPage as IDisposable).Dispose(); } } }
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); } }