November Happy Hour will be moved to Thursday December 5th.

XhtmlString ToHtmlString and internal links (Episerver 11.8.1)

Vote:
 

Hi!

When calling ToHtmlString on a XhtmlString property, the internal links are not rewritten. They are in ~/link/cdc1e0bb0f854b579fced7ded775e568.aspx format. How can I solve this?

I tried to do an ugly workaround like this, but then I get the CMS edit URL for the page, which leads to anonymous users being redirected to login page:

private static string MergeStringFragments(IEnumerable stringFragments)
{
    StringBuilder stringBuilder = new StringBuilder();

    foreach (IStringFragment stringFragment in stringFragments)
    {
        UrlFragment urlFragment = stringFragment as UrlFragment;

        if (urlFragment != null)
        {
            var content = UrlResolver.Current.Route(new UrlBuilder(urlFragment.Url), ContextMode.Default);

            if (content != null)
            {
                stringBuilder.Append(UrlResolver.Current.GetUrl(content.ContentLink, null, new VirtualPathArguments { ContextMode = ContextMode.Default }));
                continue;
            }
        }

        stringBuilder.Append(stringFragment.GetViewFormat());
    }

    return stringBuilder.ToString();
}

Can someone please explain why I get the edit URL (eg. /EPiServer/CMS/Content/mylink,,30/) when calling UrlResolver.Current.GetUrl(content.ContentLink, null, new VirtualPathArguments { ContextMode = ContextMode.Default })?

The scenario is I'm returning JSON from a WebAPI controller.

#193895
Jun 07, 2018 10:43
Vote:
 

Oh, btw, just found the root reason to why I had to replace XhtmlString with string in my view model. This comes from built-in XhtmlStringJsonConverter:

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
  XhtmlString xhtmlString = value as XhtmlString;
  if (xhtmlString == null)
    writer.WriteValue(string.Empty);
  else
    writer.WriteValue(xhtmlString.ToEditString());
}

It can't be intended to always use .ToEditString() in this case, right?

#193899
Edited, Jun 07, 2018 11:03
Vote:
 
<p>The json converters that are registered in IOC container are implemented for the serialization used for edit (as you detected in&nbsp;the example above). Looking back it would probably have been better if those converters where registered in a more specific edit way (e.g. some interface specific for edit) and not just as JsonConvert in IOC container. The problem with having "generic" converters in IOC container is that the format could be different depending on the usecase (database format, edit format, view format etc).</p>
#193909
Jun 07, 2018 14:43
Vote:
 

Ok, but do you have any idea of how I can get rewritten URLs when calling ToHtmlString? Is this a bug?

#193913
Jun 07, 2018 14:54
Vote:
 

Which formatter/serializer is your WebAPI using? To me it seems that it somehow fetches all registered JsonConverters from IOC container and registers them, XhtmlString it self does not have any JsonConverter attribute.

#193942
Jun 08, 2018 9:41
Vote:
 

Here is a link to a post on how you can "render" an xhtml string in code. Except from links can their be content items (like blocks) and personalized content in the xhtmlstring so how to "render" it depends on which format you want from your web api.

#193943
Jun 08, 2018 9:49
Vote:
 

I just stumbled on this as well that using .ToHtmlString() on a XhtmlProperty in a be controller to be sent to fe for rendering and internal links is rendered in "internal style .aspx". Running 12.13.

Mattias Olsson how did you solve your issue?

#296866
Feb 20, 2023 13:46
* 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.