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); }
/// <summary> /// 渲染一个Razor视图模板 /// </summary> /// <param name="context">HttpContextBase实例引用</param> /// <param name="pageVirtualPath">Razor视图的路径</param> /// <param name="model">要渲染到视图上的数据对象</param> /// <returns>渲染后的HTML代码</returns> protected virtual string RenderPage(HttpContextBase context, string pageVirtualPath, object model) { // 扩展点:如果需要实现页面替换逻辑,例如个性化页面覆盖标准产品页面,可以重写这个方法 WebPageBase page = WebPage.CreateInstanceFromVirtualPath(pageVirtualPath); StringWriter output = new StringWriter(); WebPageContext pageContext = new WebPageContext(context, null, model); page.ExecutePageHierarchy(pageContext, output); return(output.ToString()); }
/// <summary> /// Initializes a module and prepares it to handle requests. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application </param> public void Init(HttpApplication context) { const string errorPage = "~/Views/Shared/Error.cshtml"; // When ever an error occurs on the Http Application, we'll now handle it, here. context.Error += (sender, e) => { HttpContext.Current.ClearError(); IWebObjectFactory webObjectFactory = BuildManager.GetObjectFactory(errorPage, true); WebPageBase webPageBase = webObjectFactory.CreateInstance() as WebPageBase; if (webPageBase == null) { throw new InvalidOperationException("Failed to create an instance of the following page: " + errorPage); } var wrapper = new HttpContextWrapper(HttpContext.Current); var webPageContext = new WebPageContext(wrapper, null, null); webPageBase.ExecutePageHierarchy(webPageContext, HttpContext.Current.Response.Output); }; }
/// <summary> /// Executes the razor page. /// </summary> /// <param name="webPage">The web page.</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( WebPageBase webPage, Action<WebPageBase> setParameters, Type resultType, FunctionContextContainer functionContextContainer) { HttpContext currentContext = HttpContext.Current; var startPage = StartPage.GetStartPage(webPage, "_PageStart", new[] { "cshtml" }); // IEnumerable<PageExecutionListener> pageExecutionListeners; HttpContextBase httpContext; if (currentContext == null) { httpContext = new NoHttpRazorContext(); // pageExecutionListeners = new PageExecutionListener[0]; } else { httpContext = new HttpContextWrapper(currentContext); // pageExecutionListeners = httpContext.PageInstrumentation.ExecutionListeners; } var pageContext = new WebPageContext(httpContext, webPage, startPage); if (functionContextContainer != null) { pageContext.PageData.Add(PageContext_FunctionContextContainer, functionContextContainer); } if (setParameters != null) { setParameters(webPage); } var sb = new StringBuilder(); using (var writer = new StringWriter(sb)) { //// PageExecutionContext enables "Browser Link" support //var pageExecutionContext = new PageExecutionContext //{ // TextWriter = writer, // VirtualPath = PathUtil.Resolve(webPage.VirtualPath), // StartPosition = 0, // IsLiteral = true //}; //pageExecutionListeners.ForEach(l => l.BeginContext(pageExecutionContext)); webPage.ExecutePageHierarchy(pageContext, writer); //pageExecutionListeners.ForEach(l => l.EndContext(pageExecutionContext)); } string output = sb.ToString(); if (resultType == typeof(XhtmlDocument)) { if (string.IsNullOrWhiteSpace(output)) return new XhtmlDocument(); try { return XhtmlDocument.ParseXhtmlFragment(output); } catch (XmlException ex) { string[] codeLines = output.Split(new [] { Environment.NewLine, "\n" }, StringSplitOptions.None); XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, codeLines, ex.LineNumber); throw; } } return ValueTypeConverter.Convert(output, resultType); }