Esempio n. 1
0
        private static XDocument RenderInternal(string path)
        {
            var responseWriter = new StringWriter();

            var mvcResponse = new MvcPlayerHttpResponseWrapper(responseWriter, PageRenderer.CurrentPageId);
            var mvcRequest  = new MvcPlayerHttpRequestWrapper(Request, path);
            var mvcContext  = new MvcPlayerHttpContextWrapper(Context, mvcResponse, mvcRequest);

            lock (HttpContext.Current)
            {
                new MvcHttpHandlerWrapper().PublicProcessRequest(mvcContext);
            }

            // Redirects
            if (!string.IsNullOrEmpty(mvcResponse.RedirectLocation))
            {
                Response.Redirect(mvcResponse.RedirectLocation, false);
                Response.Flush();
                Response.Close();
                return(null);
            }

            // Ajax requests
            if ((mvcRequest.IsAjaxRequest() || !mvcResponse.IsHtmlResponse()) && path == PathInfo)
            {
                byte[] bytes = Response.ContentEncoding.GetBytes(responseWriter.ToString());

                var page = Context.Handler as System.Web.UI.Page;
                if (page != null)
                {
                    page.PreRender += (a, b) =>
                    {
                        Response.ClearContent();
                        Response.ClearHeaders();
                        Response.ContentType = mvcResponse.ContentType;
                        Response.AddHeader("Content-Length", bytes.Length.ToString());
                        Response.AddHeader("Content-Encoding", "none");
                        Response.BinaryWrite(bytes);
                        Response.Flush();
                        Response.End();
                    };
                    return(null);
                }
            }

            var sbHtml = new StringBuilder();

            sbHtml
            .Append(@"<html xmlns=""http://www.w3.org/1999/xhtml""> 
                <head/>
                <body>")
            .Append(responseWriter.ToString())
            .Append(@"
                </body>
                </html>");

            try
            {
                return(XDocument.Parse(sbHtml.ToString()));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("MvcPlayer: failed to parse result markup as xml", ex);
            }
        }
Esempio n. 2
0
		private static XDocument RenderInternal(string path)
		{
			var responseWriter = new StringWriter();

			var mvcResponse = new MvcPlayerHttpResponseWrapper(responseWriter, PageRenderer.CurrentPageId);
			var mvcRequest = new MvcPlayerHttpRequestWrapper(Request, path);
			var mvcContext = new MvcPlayerHttpContextWrapper(Context, mvcResponse, mvcRequest);

			lock (HttpContext.Current)
			{
				new MvcHttpHandlerWrapper().PublicProcessRequest(mvcContext);
			}

			// Redirects
			if (!string.IsNullOrEmpty(mvcResponse.RedirectLocation))
			{
				Response.Redirect(mvcResponse.RedirectLocation, false);
				Response.Flush();
				Response.Close();
				return null;
			}

			// Ajax requests
			if ((mvcRequest.IsAjaxRequest() || !mvcResponse.IsHtmlResponse()) && path == PathInfo)
			{
				byte[] bytes = Response.ContentEncoding.GetBytes(responseWriter.ToString());

				var page = Context.Handler as System.Web.UI.Page;
				if (page != null)
				{
					page.PreRender += (a, b) =>
					{
						Response.ClearContent();
						Response.ClearHeaders();
						Response.ContentType = mvcResponse.ContentType;
						Response.AddHeader("Content-Length", bytes.Length.ToString());
						Response.AddHeader("Content-Encoding", "none");
						Response.BinaryWrite(bytes);
						Response.Flush();
						Response.End();
					};
					return null;
				}

			}

			var sbHtml = new StringBuilder();
			sbHtml
				.Append(@"<html xmlns=""http://www.w3.org/1999/xhtml""> 
                <head/>
                <body>")
				.Append(responseWriter.ToString())
				.Append(@"
                </body>
                </html>");

			try
			{
				return XDocument.Parse(sbHtml.ToString());
			}
			catch (Exception ex)
			{
				throw new InvalidOperationException("MvcPlayer: failed to parse result markup as xml", ex);
			}
		}