Ejemplo n.º 1
0
        private XhtmlDocument ParseOutput(string xhtml)
        {
            try
            {
                return(XhtmlDocument.ParseXhtmlFragment(xhtml));
            }
            catch (XmlException ex)
            {
                string[] codeLines = xhtml.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None);

                XhtmlErrorFormatter.EmbedSourceCodeInformation(ex, codeLines, ex.LineNumber);

                throw;
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }