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) } }
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; }
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()); }
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.
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...
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 :)
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.
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