Contentful logo

Contentful Community

Rich Text embedded custom content Render error .NET

I am trying to follow the .NET rich text tutorial (Webforms) to add a custom renderer to the htmlRenderer, but am receiving an error as it does not seem serialize the target entry properly. Thought it would be pretty simple and straightforward… maybe I’m missing something simple.

The error I receive is " Object reference not set to an instance of an object." - because the result either wasn’t found or didn’t serialize properly. Error comes when I try to display model.Title. The class model is correct as it’s the same content model as the entry that contains the embedded rich text (which is working properly).

Code below for Custom Content Renderer:

public class ContentfulDocEmbed : IContent
{
public SystemProperties sys { get; set; }
public string Title { get; set; }
public Document Body { get; set; }
public string ShowId { get; set; }
}

    public class CustomContentRenderer : IContentRenderer
    {
        public int Order { get; set; }

        public bool SupportsContent(IContent content)
        {
            return content is EntryStructure && (content as EntryStructure).NodeType == "embedded-entry-block";
        }


        public string Render(IContent content)
        {
            var model = (content as EntryStructure).Data.Target as ContentfulDocEmbed; 
            var sb = new StringBuilder();

            sb.Append("<div>");
            sb.Append($"<h2>{model.Title} title ---</h2>");
            sb.Append("</div>");

            return sb.ToString();
        }

        public Task<string> RenderAsync(IContent content)
        {
            return Task.FromResult(Render(content));
        }


    }
}

Hi @ckelley,

May I ask which tutorial or Github repository are you using? From the error you mentioned, it seems that the rich-text is either not well transformed into the object that you are suppose to manipulate, or that the render is not instantiated correctly.