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


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 to it's friendly url of the form

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.


Mar 04, 2016 0:31
Mar 04, 2016 15:50

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)

Edited, Mar 07, 2016 2:42

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;
                result = writer.ToString();
            return result;
Mar 07, 2016 9:48

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());
Mar 07, 2016 10:01

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.

Mar 07, 2016 22:28

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...

Mar 07, 2016 22:38

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

Mar 07, 2016 23:29

Ok for 7 you can check out

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 :)

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.