Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Elliott Brown 40 posts 210 karma points
    Jul 29, 2019 @ 09:50
    Elliott Brown
    0

    Why does Umbraco treat IPublishedContent differently when called via AJAX

    The short story is I have a list of blog articles which I'm casting to a model which contains .wordCount. This is calculated by striping the HTML from the output of the RTE and then calculating the lengh. The output of the RTE can contain Macros.

    On first load, i.e from a view, the output of the RTE renders my Macros as HTML. However, when I refresh my list articles using a clientside AJAX the output of the RTE is rendered differently. My Macros now look like this: <?UMBRACO_MACRO macroAlias="ArticleAudio" audioPicker="6068" audioPosition="left" audioTitle="Interview Audio" />

    I also get the error System.InvalidOperationException: 'Cannot render a macro when there is no current PublishedContentRequest.

    public static Article ToArticle(this IPublishedContent item)
    {
        string rawText = HelperFunctions.StripHTML(item.GetPropertyValue<string>("richText"));
        ...
    }
    
    public static string StripHTML(string htmlString)
    {
        string pattern = @"<(.|\n)*?>";
        return Regex.Replace(htmlString, pattern, string.Empty);
    }
    

    Expected

    <div class="audio-player">
        <audio style="display: none;">
            <source src="https://www.address.com/media/77390/interview.mp3" type="audio/mpeg">
            Your browser does not support the audio element.
        </audio>
    </div>
    

    My Output

    <?UMBRACO_MACRO macroAlias="ArticleAudio" audioPicker="6068" audioPosition="left" audioTitle="Interview Audio" />
    

    Thank you in advance for your help

Please Sign in or register to post replies

Write your reply to:

Draft