Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Get content from XHTML property so internal URLs are converted to friendlyURLs

Vote:
 

We have a web forms site that has a webservice exposing certain properties from any given page.  Is all working fine except I want internally stored URLs in an XHTML property to be converted from http://mysite.com/templates/page.aspx?id=555 to it's friendly url of the form http://mysite.com/about/people/john.

The following returns the value without the friendlyURLs converted.

 _pageContent = pd.Property["MainBody"].Value.ToString()

I'm looking for a lightweight way of doing this without having to parse the urls and rewrite them individually. 

Any suggestions would be appreicated.

PK
  

#145450
Mar 04, 2016 0:31
Vote:
 
#145502
Mar 04, 2016 15:50
Vote:
 

Thanks for the pointer.  

I haven't had any joy following through with implementing this - wondered if you are able to give a code example.  

In fact, the RenderXhtmlString helper method looks more of what I want to latch on to.  I just don't understand how to get the output from calling the helper method when I'm not actually in the response render.

My web service is gathering certain properties and returning them in its own serialized object.  If the property is an Xhtml string I want the object's value to be the rendered Html.  So, I'd want something like this to happen - but don't know how to effect it:

public class PageProperties
    {
        public readonly int PageID, ParentID;
        public readonly string Title, Content

        private PageProperties() { }


        public PageProperties(PageData pd)
        {
            PageID = pd.ContentLink.ID;
            ParentID = pd.ParentLink.ID;
            Title = pd.Property["Title"].Value.ToString();
            
                XhtmlString xString =  (XhtmlString)pd.Property["MainBody"].Value;

                // here's where I can't figure out how to Get the value of the Xhtml string after rendering.  How do I make a call to RenderXhtmlString and get a return value
                // I realise the next line is flawed - any help appreciated.    ?!? 
                Content = RenderXhtmlString(htmlHelper, xString)
        }

    }
#145559
Edited, Mar 07, 2016 2:42
Vote:
 

If you only need to convert URL:s you could go through all fragments of the Xhtml-string and convert the UrlFragments. If you need to render blocks, dynamic content etc you can use the method below. Not sure if it's the best solution, but it seems to do the work.. :) 

public static string ConvertToExternal(this XhtmlString xhtmlstring)
        {
            if (string.IsNullOrWhiteSpace(xhtmlstring?.ToHtmlString()) || xhtmlstring.IsEmpty)
            {
                return string.Empty;
            }
            var routeData = new RouteData();
            routeData.Values.Add("controller", "a");
            routeData.Values.Add("subject", "a");

            var hh = new HtmlHelper(new ViewContext()
            {
                HttpContext = new HttpContextWrapper(HttpContext.Current),
                ViewData = new ViewDataDictionary(),
                TempData = new TempDataDictionary(),
                Controller = new DummyController(),
                RouteData = routeData
            }, new ViewPage());


            string result;
            using (var writer = new StringWriter())
            {
                hh.ViewContext.Writer = writer;
                hh.RenderXhtmlString(xhtmlstring);
                writer.Flush();
                result = writer.ToString();
            }
            return result;
        }
#145563
Mar 07, 2016 9:48
Vote:
 

The code to convert just links was on line above.. :)

public static XhtmlString ToExternalLinks(this XhtmlString xhtmlString)
        {
            var result = new StringBuilder();
            foreach (var fragment in xhtmlString.Fragments)
            {
                var urlFragment = fragment as UrlFragment;
                result.Append(urlFragment != null ? UrlResolver.Current.GetUrl(new UrlBuilder(urlFragment.InternalFormat), ContextMode.Default) : fragment.InternalFormat);
            }

            return new XhtmlString(result.ToString());
        }
#145564
Mar 07, 2016 10:01
Vote:
 

Thanks Erik.  This certainly works and, giving me rendered dynamic controls is better than I would have hoped (However, that could be a double-edged sword down the track :-(  )

If that turns out to be the case I will look at converting just links - although when I tried that initially,  the UrlResolver complained that it did not have a Method or property "Current".

Many thanks for your help.

#145589
Mar 07, 2016 22:28
Vote:
 

Current is a property that came in later versions. You can use the ServiceLocator to grab it instead. What version are you running? If you have 7 you won't have GetUrl method either so you will need a bit extra work for that external url...

#145590
Mar 07, 2016 22:38
Vote:
 

Thanks Daniel.  Yes, we are only on 7 flat.  Politics abound! 

#145593
Mar 07, 2016 23:29
Vote:
 

Ok for 7 you can check out 

https://andersnordby.wordpress.com/2014/04/29/getting-an-external-url-in-episerver-7-x/

For external url. Also got an example of how to get UrlResolver from ServiceLocator.

I would also recommend upgrading to newest version really. Episerver 7 is still too buggy. At least to last release of 7 will make it much more stable. But since upgrading from there to 9 is pretty easy I would recommend going all the way...

Episerver 8+ rocks :)

#145614
Mar 08, 2016 11:38
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.