Try our conversational search powered by Generative AI!

Error displaying internal links from XhtmlString instance using .ToHtmlString()


I’m trying to use the .ToHtmlString() method on an instance of XhtmlString, but internal links does not render correctly. Is there another way to convert an XhtmlString to a string? I’m trying to use the .ToHtmlString() in the context of a controller, so I cannot use the HtmlHelper extension method .XhtmlString(...) without creating a helper instance and I don’t want to do that.

This is in MVC.


Apr 17, 2013 16:15

While I don't necessarily know of an EPi-way, nor do I know the full context of your situation, we can sort of hack this together so it works...

public ActionResult Index(StandardPage currentPage)
    var originalString = currentPage.MainBody;

    HtmlHelper htmlHelper = new HtmlHelper(new ViewContext(ControllerContext, new RazorView(ControllerContext, "nothing", null, false, null), ViewData, TempData, new StringWriter()), new ViewPage());
    var convertedString = htmlHelper.XhtmlString(currentPage.MainBody).ToString();

    return View(currentPage);

Essentially what's it does is just create an HtmlHelper in the Controller, so then you at least have access to .XhtmlString(); This was pieced together and updated from this StackOverflow post. It might not be the best way, but it is at least one way to do it...

Edited, Apr 18, 2013 1:52

I was looking at the option of creating a HtmlHelper, but it just feels wrong with all the object instantiations needed in addition to all the nulls. Thanks, though! I appreciate the tip.

I guess my real question is: Is there a bug in the XhtmlString.ToHtmlString() method? I have seen it mentioned here in the forums earlier that it is supposed to resolve dynamic contentet rendering as well, while in the same thread it it stated that it doesn’t do it.

Apr 18, 2013 8:22

Any news on this? 

Nov 05, 2013 11:36

@Per Hemmingson:

While we wait for upgrade to cms 7.5, this is a working solution in my case:


public static string ToHtmlStringDoneRight(this XhtmlString instance)
    if (null == instance)
        return null;
    if (instance.IsEmpty)
        return String.Empty;
    StringBuilder stringBuilder = new StringBuilder();
    foreach (IStringFragment stringFragment in instance.Fragments)
        UrlFragment urlFragment = stringFragment as UrlFragment;
        if (null != urlFragment)
            Uri externalUrl;
            if (TryGetExternalUrl(urlFragment.InternalFormat, out externalUrl))
    return stringBuilder.ToString();
private static bool TryGetExternalUrl(string internalFormat, out Uri externalUrl)
        //The call to GetExternal URL here is a wrapper around a call to EPiServer.Global.UrlRewriteProvider.ConvertToExternal.
        externalUrl = (new Url(internalFormat)).GetExternalUrl();
    catch (Exception)
        externalUrl = null;
    return null != externalUrl;


Edited, Feb 14, 2014 17:30
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.