Skip to content

Commit 5570583

Browse files
authored
Fixes issue with macro rendering in an RTE when GUIDs are used for backoffice document routes (#18691)
* Fixes issue with macro rendering in an RTE when GUIDs are used for backoffice document routes. * Fixed null reference error.
1 parent eb97962 commit 5570583

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

src/Umbraco.Web.BackOffice/Controllers/MacroRenderingController.cs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public ActionResult<IEnumerable<MacroParameter>> GetMacroParameters(int macroId)
8787
[HttpGet]
8888
public async Task<IActionResult> GetMacroResultAsHtmlForEditor(string macroAlias, int pageId,
8989
[FromQuery] IDictionary<string, object> macroParams) =>
90-
await GetMacroResultAsHtml(macroAlias, pageId, macroParams);
90+
await GetMacroResultAsHtml(macroAlias, pageId.ToString(), macroParams);
9191

9292
/// <summary>
9393
/// Gets a rendered macro as HTML for rendering in the rich text editor.
@@ -98,11 +98,24 @@ public async Task<IActionResult> GetMacroResultAsHtmlForEditor(string macroAlias
9898
/// <param name="model"></param>
9999
/// <returns></returns>
100100
[HttpPost]
101+
[NonAction]
102+
[Obsolete("This endpoint is no longer used.")]
101103
public async Task<IActionResult> GetMacroResultAsHtmlForEditor(MacroParameterModel model) =>
104+
await GetMacroResultAsHtml(model.MacroAlias, model.PageId.ToString(), model.MacroParams);
105+
106+
/// <summary>
107+
/// Gets a rendered macro as HTML for rendering in the rich text editor.
108+
/// Using HTTP POST instead of GET allows for more parameters to be passed as it's not dependent on URL-length
109+
/// limitations like GET.
110+
/// The method using GET is kept to maintain backwards compatibility
111+
/// </summary>
112+
/// <param name="model"></param>
113+
/// <returns></returns>
114+
[HttpPost]
115+
public async Task<IActionResult> GetMacroResultAsHtmlForEditor(MacroParameterModel2 model) =>
102116
await GetMacroResultAsHtml(model.MacroAlias, model.PageId, model.MacroParams);
103117

104-
private async Task<IActionResult> GetMacroResultAsHtml(string? macroAlias, int pageId,
105-
IDictionary<string, object>? macroParams)
118+
private async Task<IActionResult> GetMacroResultAsHtml(string? macroAlias, string pageId, IDictionary<string, object>? macroParams)
106119
{
107120
IMacro? m = macroAlias is null ? null : _macroService.GetByAlias(macroAlias);
108121
if (m == null)
@@ -111,11 +124,11 @@ private async Task<IActionResult> GetMacroResultAsHtml(string? macroAlias, int p
111124
}
112125

113126
IUmbracoContext umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
114-
IPublishedContent? publishedContent = umbracoContext.Content?.GetById(true, pageId);
127+
IPublishedContent? publishedContent = GetPagePublishedContent(pageId, umbracoContext);
115128

116129
//if it isn't supposed to be rendered in the editor then return an empty string
117130
//currently we cannot render a macro if the page doesn't yet exist
118-
if (pageId == -1 || publishedContent == null || m.DontRender)
131+
if (publishedContent == null || m.DontRender)
119132
{
120133
//need to create a specific content result formatted as HTML since this controller has been configured
121134
//with only json formatters.
@@ -149,6 +162,21 @@ private async Task<IActionResult> GetMacroResultAsHtml(string? macroAlias, int p
149162
}
150163
}
151164

165+
private static IPublishedContent? GetPagePublishedContent(string pageId, IUmbracoContext umbracoContext)
166+
{
167+
if (int.TryParse(pageId, NumberStyles.Integer, CultureInfo.InvariantCulture, out int pageIdAsInt))
168+
{
169+
return umbracoContext.Content?.GetById(true, pageIdAsInt);
170+
}
171+
172+
if (Guid.TryParse(pageId, out Guid pageIdAsGuid))
173+
{
174+
return umbracoContext.Content?.GetById(true, pageIdAsGuid);
175+
}
176+
177+
return null;
178+
}
179+
152180
[HttpPost]
153181
public IActionResult CreatePartialViewMacroWithFile(CreatePartialViewMacroWithFileModel model)
154182
{
@@ -180,13 +208,21 @@ public IActionResult CreatePartialViewMacroWithFile(CreatePartialViewMacroWithFi
180208
return Ok();
181209
}
182210

211+
[Obsolete("This model is no longer used and has been replaced with MacroParameterModel2 that changes the type of the PageId property.")]
183212
public class MacroParameterModel
184213
{
185214
public string? MacroAlias { get; set; }
186215
public int PageId { get; set; }
187216
public IDictionary<string, object>? MacroParams { get; set; }
188217
}
189218

219+
public class MacroParameterModel2
220+
{
221+
public string? MacroAlias { get; set; }
222+
public string PageId { get; set; } = string.Empty;
223+
public IDictionary<string, object>? MacroParams { get; set; }
224+
}
225+
190226
public class CreatePartialViewMacroWithFileModel
191227
{
192228
public string? Filename { get; set; }

0 commit comments

Comments
 (0)