Error rendering custom macro inside Rich Text Editor: Value cannot be null. (Parameter 'content')

Hi,
I have a problem to rendering macro added to Rich Text Editor. After adding my custom macro to insert .mp4 video inside html, and I try to get value of this Rich Text Editor with this function content.Value<string>("description", culture), it return an error like this:

"message": "Value cannot be null. (Parameter 'content') ",
"stackTrace": "   at Umbraco.Cms.Web.Common.Macros.MacroRenderer.RenderAsync(MacroModel macro, IPublishedContent content)\r\n   at Umbraco.Cms.Web.Common.Macros.MacroRenderer.RenderAsync(String macroAlias, IPublishedContent content, IDictionary`2 macroParams)\r\n   at Umbraco.Cms.Core.PropertyEditors.ValueConverters.RteMacroRenderingValueConverter.<>c__DisplayClass7_0.<RenderRteMacros>b__1(String macroAlias, Dictionary`2 macroAttributes)\r\n   at Umbraco.Cms.Infrastructure.Macros.MacroTagParser.ParseMacros(String text, Action`1 textFoundCallback, Action`2 macroFoundCallback)\r\n   at Umbraco.Cms.Core.PropertyEditors.ValueConverters.RteMacroRenderingValueConverter.RenderRteMacros(String source, Boolean preview)\r\n   at Umbraco.Cms.Core.PropertyEditors.ValueConverters.RteMacroRenderingValueConverter.Convert(Object source, Boolean preview)\r\n   at Umbraco.Cms.Core.PropertyEditors.ValueConverters.RteMacroRenderingValueConverter.ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, Object inter, Boolean preview)\r\n   at Umbraco.Cms.Core.Models.PublishedContent.PublishedPropertyType.ConvertInterToObject(IPublishedElement owner, PropertyCacheLevel referenceCacheLevel, Object inter, Boolean preview)\r\n   at Umbraco.Cms.Infrastructure.PublishedCache.Property.GetValue(String culture, String segment)\r\n   at Umbraco.Extensions.PublishedPropertyExtension.Value[T](IPublishedProperty property, IPublishedValueFallback publishedValueFallback, String culture, String segment, Fallback fallback, T defaultValue)\r\n   at Umbraco.Extensions.PublishedContentExtensions.Value[T](IPublishedContent content, IPublishedValueFallback publishedValueFallback, String alias, String culture, String segment, Fallback fallback, T defaultValue)\r\n   at Umbraco.Extensions.FriendlyPublishedContentExtensions.Value[T](IPublishedContent content, String alias, String culture, String segment, Fallback fallback, T defaultValue)\r\n   at UmbracoStandard.S4W.API.Models.Objects.NewsDetailDto.Map(IPublishedContent content, String culture, List`1 activeCultures, GenericSeo genericSeo, SeoPerDocument seoPerDocument) in C:\\GitProjects\\Zinelli e Perizzi\\Nuxt-Umbraco website\\umbraco\\UmbracoStandard\\S4W\\API\\Models\\Objects\\News\\NewsDetailDto.cs:line 47\r\n   at UmbracoStandard.S4W.API.Services.Mappers.Implementors.NewsDetailMapper.MapDetail(IPublishedContent content, IPublishedContent prev, IPublishedContent next, String culture) in C:\\GitProjects\\Zinelli e Perizzi\\Nuxt-Umbraco website\\umbraco\\UmbracoStandard\\S4W\\API\\Services\\Mappers\\Implementors\\NewsDetailMapper.cs:line 39\r\n   at UmbracoStandard.S4W.API.Controllers.NewsMarineDivisionController.GetDetail(String slug, String culture) in C:\\GitProjects\\Zinelli e Perizzi\\Nuxt-Umbraco website\\umbraco\\UmbracoStandard\\S4W\\API\\Controllers\\NewsMarineDivisionController.cs:line 41\r\n   at lambda_method6852(Closure , Object , Object[] )\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()\r\n--- End of stack trace from previous location ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()\r\n--- End of stack trace from previous location ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)"

This is macro partial view:

@inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage;

@if (Model.MacroParameters["chooseAVideo"] != null)
{
    var video = Model.MacroParameters["chooseAVideo"];
    <video controls autoplay width="500" height="300" src="@video"></video>
}

The macro work correctly in the text editor preview:

Umbraco version: 9.3.1

What am I doing wrong?

Thanks for your help.


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/110065-error-rendering-custom-macro-inside-rich-text-editor-value-cannot-be-null-parameter-content

I’m getting the following error when accessing an RTE property that contains macros through Models Builder in a BackOffice API controller:

System.ArgumentNullException: Value cannot be null. (Parameter 'content')
   at Umbraco.Cms.Web.Common.Macros.MacroRenderer.RenderAsync(MacroModel macro, IPublishedContent content)

Context:

  • Using Umbraco v13

  • Working in a BackOffice controller inheriting from UmbracoAuthorizedApiController. I am collecting content to send via API to a search index SAAS platform.

  • Accessing an RTE property via Models Builder: myContent.MyRteProperty

  • The RTE contains macros that need to be rendered

  • I have access to the IPublishedContent object but don’t know how to provide it as context for macro rendering

  • I’m not manually calling any macro rendering code - this happens automatically when accessing the RTE property

The Problem: When accessing RTE properties through Models Builder in a BackOffice API controller, Umbraco’s RteMacroRenderingValueConverter tries to render embedded macros but can’t find the required IPublishedContent context that would normally be available during regular page rendering.

Question: How can I manually specify the content context so that macros in RTE properties render correctly when accessed from a UmbracoAuthorizedApiController in Umbraco v13?