0

is it possible to render a specific page in a razor function. I tried @RenderPage but i cant figure out the path. Are there any built in functions to accomplish this?

Thanks Johan

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
Johan
  • 1
  • 1

1 Answers1

0

Not really a C1 specific approach, but personally my best approach has been to just make a separate web-request for the page in question, parse out the html and render it.

This code can serve as an example, its a 1:1 of what i'm using. As you can see the trick is to find the element that wraps your content, in my example its the element inside that has an id equals to ContentColumnInner

public static string GetContentFromPage(Guid pageId)
{
    var DomainName = HttpContext.Current.Request.Url.Authority;
    var Uri = String.Format("http://{0}/page({1})", DomainName, pageId);
    var request = WebRequest.Create(Uri);

    // If required by the server, set the credentials.
    request.Credentials = CredentialCache.DefaultCredentials;

    // Get the response.
    using (var response = (HttpWebResponse)request.GetResponseWithoutException())
    {
        if (response.StatusCode != HttpStatusCode.OK)
        {
            LogError("StatusCode: " + response.StatusCode);
            return null;
        }

        // Get the stream containing content returned by the server.
        using (var responseStream = response.GetResponseStream())
        {
            if (responseStream == null)
            {
                LogError("ResponseStream is null");
                return null;
            }

            // Open the stream using a StreamReader for easy access.
            using (var stream = new StreamReader(responseStream))
            {
                // Read the content.
                var responseFromServer = stream.ReadToEnd();
                var beforeBodyStartIndex = responseFromServer.IndexOf("<body", StringComparison.Ordinal);
                var afterBodyEndIndex = responseFromServer.LastIndexOf("</body>", StringComparison.Ordinal) + 7;
                var body = responseFromServer.Substring(beforeBodyStartIndex, afterBodyEndIndex - beforeBodyStartIndex);

                try
                {
                    var xmlDocument = XElement.Parse(body);

                    var content = xmlDocument.Descendants().FirstOrDefault(o => o.Attribute("id") != null && o.Attribute("id").Value.EndsWith("ContentColumnInner"));

                    if (content == null || !content.HasElements)
                    {
                        return null;
                    }

                    var reader = content.CreateReader();
                    reader.MoveToContent();

                    return reader.ReadInnerXml();
                }
                catch (XmlException ex)
                {
                    LogError("Error parsing xml: " + ex.Message);
                    return null;
                }
            }
        }
    }
}
Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48