Page Renderer on MVC 3

Aug 11, 2011 at 10:12 AM

Hi all,

We are trying to render a page using the API from an external MVC application (in another application pool). We have a problem trying to use the PageRenderer.render method, maybe it is quite obvious but we cannot figure out why is failing and if there is a nice solution. We made a workaround to skip this problem creating our own renderer which is getting the templates, parsing the content and doing more things in order to get all the content right but this is not nice and difficult to test and it could be a nightmare in case we upgrade the version of compositec1 which probably we’ll do, so we are quite interested in use the API renderer to save us some problems in the future.


Thank you very much for any help on this.

Our controller looks like this :

public ActionResult GetIdealRenderContent()

{

var sb = new StringBuilder();

var control = new Control();

using (var connection = new DataConnection())

{

var myPage = connection.Get<IPage>().Where(page => page.FriendlyUrl == TestFriendlyUrl).First();

IEnumerable<IPagePlaceholderContent> myContents = connection.Get<IPagePlaceholderContent>().Where(con => con.PageId == myPage.Id);

PageRenderer.CurrentPage = myPage;

var sb2 = new StringBuilder();

var tw = new StringWriter(sb2);

var hw = new HtmlTextWriter(tw);

control = PageRenderer.Render(myPage, myContents);

control.RenderControl(hw);

sb.Append(sb2.ToString());

}

ViewBag.result = sb.ToString();

return View("viewContent");

}

And the view is :

Get Content

@Html.Raw(ViewBag.result)

The error we got is :

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 290: control = PageRenderer.Render(myPage, myContents);

Line 291:

Line 292: control.RenderControl(hw);

Line 293: sb.Append(sb2.ToString());

Line 294:


Source File: D:\Repo\CompositeC1Demo\Demo.TestCMSApi\Controllers\HomeController.cs Line: 292

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]

System.Web.UI.HtmlControls.HtmlHead.RenderChildren(HtmlTextWriter writer) +284

System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +31

System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27

System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100

System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25

System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208

System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8

System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +31

System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27

System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100

System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25

System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208

System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8

System.Web.UI.Control.Render(HtmlTextWriter writer) +10

System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27

System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100

System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25

Demo.TestCMSApi.Controllers.HomeController.GetIdealRenderContent() in D:\ Repo\CompositeC1Demo\Demo.TestCMSApi\Controllers\HomeController.cs:292

Aug 11, 2011 at 11:40 AM

you should NOT use the PageRender.Render method for MVC since it relies on WebForms. Instead, if you just need all the html of a rendered page, you should make a WebRequest that emulates a normal browser requesting a page, and then use the Response for further processing in your MVC app.

Aug 11, 2011 at 2:48 PM

Hi burningice,

Thanks for your suggestion.

We've thought about make a web request but we thought we did something wrong and for this reason did not work. At least now we know the way to go.

Thanks again