1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Get Complete Page Content

Get Complete Page Content

3 posts, 0 answered
  1. Luc Baeten
    Luc Baeten avatar
    169 posts
    Registered:
    23 Sep 2005
    13 Mar 2008
    Link to this post
    Hello Sitefinity Team

    I have defined a pluggable module. With this module I want to have the complete content of a page that is defined in Sitefinity CMS. And use that content as HTML page.

    What I used till now is:

      HttpWebRequest wbrq; 
      HttpWebResponse wbrs;
      StreamReader sr;

      // Create the web request
      wbrq = (HttpWebRequest)WebRequest.Create(cnt.NavigateUrl);
      wbrq.Method = "GET";

      //Read the rerturned data
      wbrs = (HttpWebResponse)wbrq.GetResponse();
      sr = new StreamReader(wbrs.GetResponseStream());
      string strHtmlResult = sr.ReadToEnd();
      sr.Close();


    where cnt.NavigateUrl is the Full NavigateUrl (included the protocol and domain) of the defined page in Sitefinity CMS.

    This works.

    However the problem with it, is that I receive relative paths (i.e. /website/images/logo.gif) in the returned data. When I want to use this returned data as HTML page images, css etc are not shown (of course).
    I can change the master template, and all used controls to use full paths (http://localhost/website/images/logo.gif) instead of relative paths (i.e. ~/images/logo.gif).
    But maybe you know another way: for instance is it possible with the Sitefinity API to get the complete content of a CMS page? And change relative paths on the fly?

    Hope you can help

    Regards
    Luc

  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    14 Mar 2008
    Link to this post
    Hi Luc Baeten,

    It's not possible to get the full content with Absolute Paths. Still, we could use a tricky code here in order to convert all relative paths to absolute ones:

        private string CorrectRelPaths(string s,string sUrl) 
        { 
            int nIndex = 0; 
            int offset = 0; 
            int absPathIndex = 0; 
            try 
            { 
                // Find any and relative src/href's and insert the absolute path 
                while ((nIndex = s.IndexOf("src=\"",offset)) > 0) 
                { 
                    absPathIndex = s.IndexOf("src=\"http://",nIndex,12); 
                    if ( absPathIndex == -1 ) 
                        s = s.Insert(nIndex + 5, sUrl); 
                    offset = nIndex + sUrl.Length + 1; 
                } 
                offset = 0; 
                while ((nIndex = s.IndexOf("href=\"", offset)) > 0) 
                { 
                    absPathIndex = s.IndexOf("href=\"http://", nIndex,13); 
                    if ( absPathIndex == -1 )  
                       s = s.Insert(nIndex + 6, sUrl); 
                    offset = nIndex + sUrl.Length + 1; 
                } 
                offset = 0; 
                while ((nIndex = s.IndexOf("background-image:url", offset)) > 0) 
                { 
                    absPathIndex = s.IndexOf("background-image:url(http:", nIndex, 26); 
                    if ( absPathIndex == -1 )  
                       s = s.Insert(nIndex + 21, sUrl); 
                    offset = nIndex + sUrl.Length + 1; 
                } 
                 
                return s; 
            } 
            catch (Exception e) 
            { 
                return e.Message; 
            } 
        } 
     


    In this function, the sUrl variable accepts the domain that relative URLs relate to.
    So, your code should look like this at the end :
            HttpWebRequest wbrq; 
            HttpWebResponse wbrs; 
            StreamReader sr; 
            string sYourURL = cnt.NavigateUrl
            // Create the web request 
            wbrq = (HttpWebRequest)WebRequest.Create(sYourURL); 
            wbrq.Method = "GET"
            
            //Read the rerturned data 
            wbrs = (HttpWebResponse)wbrq.GetResponse(); 
            sr = new StreamReader(wbrs.GetResponseStream()); 
            string strHtmlResult = sr.ReadToEnd(); 
            sr.Close(); 
            // relative to absolute 
            strHtmlResult = CorrectRelPaths(strHtmlResult,sYourURL); 
            // Response.Write(strHtmlResult); or whatever you want
            // to do with your html result
     
     

    I hope this helps.

    Sincerely yours,
     Georgi Chokov
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Luc Baeten
    Luc Baeten avatar
    169 posts
    Registered:
    23 Sep 2005
    14 Mar 2008
    Link to this post
    Hi Georgi

    Thanks for this source. I had the same thing in mind when it was not possible to get the full content of a page via the Sitefinity API. It is a pitty that this is not possible.

    regards
    Luc
Register for webinar
3 posts, 0 answered