Fetching generated HTML



For some weid reason of acheiving SPA concept on couple of page types. I wanted to achieve the following ,If yes how

  • Receive the page Id at the WCF end point at server, sent from the client browser asynchronously (done)
  • let Episerver render a view mode html for the received pageId (don't know how)
  • Manipulate the HTML in the business logic for the end point at step 1 and send only the required html in response(will mange this step if step 2 is fulfilled)


  • Another web request to be avoided
  • If required loading the property values for the page (I can use Icontentrespository for this)
  • Don't know how but should be able to find the default template resolver for the page type
  • This is for a web form based project

Any pointer or suggestion


Amol Mahul

Aug 22, 2017 15:01

You can render your viewmodel in MVC, http://jondjones.com/learn-episerver-cms/episerver-developers-guide/episerver-customizing-episervers-ui/how-to-manually-render-a-episerver-page-or-block-using-net

public static class RenderControllerManually { public static string Render(int pageId, IEpiserverDependencies epiServerDependencies) { var viewModel = CreatePageTypesViewModel(pageId, epiServerDependencies); var viewDataDictionary = new ViewDataDictionary(viewModel); var controller = new ContentPageController(); var controllerName = GetControllerName(controller); var routeData = new RouteData(); routeData.Values.Add("Action", "Index"); routeData.Values.Add("controller", controllerName); var siteUrl = EPiServer.Configuration.Settings.Instance.SiteUrl.ToString(); var httpContext = new HttpContext( new HttpRequest(string.Empty, siteUrl, string.Empty), new HttpResponse(TextWriter.Null)); var controllerContext = new ControllerContext { RouteData = routeData, Controller = controller, HttpContext = new HttpContextWrapper(httpContext) }; var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, "~/Views/Pages/ContentPage/index.cshtml"); using (var stringWriter = new StringWriter()) { var viewContext = new ViewContext(controllerContext, viewResult.View, viewDataDictionary, new TempDataDictionary(), stringWriter); viewResult.View.Render(viewContext, stringWriter); return stringWriter.ToString(); } } private static string GetControllerName(ContentPageController controller) { var controllerName = controller.GetType().Name; var index = controllerName.IndexOf("Controller"); return index < 0 ? controllerName : controllerName.Remove(index, "Controller".Length); } private static ContentPageViewModel CreatePageTypesViewModel(int pageId, IEpiserverDependencies epiServerDependencies) { var repository = ServiceLocator.Current.GetInstance<IContentRepository>(); var contentReference = new ContentReference(pageId); var contentPage = repository.Get<ContentPage>(contentReference); return new ContentPageViewModel(contentPage, epiServerDependencies); }

Edited, Aug 22, 2017 16:22

I missed the point from your question "This is for a web form based project", Can you please let us know, which cms version you are using

Aug 22, 2017 16:26

Thanks Khurram for the input, The CMS version is 9.

Aug 22, 2017 19:56
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.