Skip to content

Commit 26382ab

Browse files
author
Paul Johnson
committed
Add EntityController GetUrlsByUdis
Enables loading multiple URLs in a single request for Media & Documents
1 parent ef7fe70 commit 26382ab

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/Umbraco.Web/Editors/EntityController.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,55 @@ public HttpResponseMessage GetUrl(Udi udi, string culture = "*")
235235
return GetUrl(intId.Result, entityType, culture);
236236
}
237237

238+
/// <summary>
239+
/// Get entity URLs by UDIs
240+
/// </summary>
241+
/// <param name="ids">
242+
/// A list of UDIs to lookup items by
243+
/// </param>
244+
/// <param name="culture">The culture to fetch the URL for</param>
245+
/// <returns>Dictionary mapping Udi -> Url</returns>
246+
/// <remarks>
247+
/// We allow for POST because there could be quite a lot of Ids.
248+
/// </remarks>
249+
[HttpGet]
250+
[HttpPost]
251+
public IDictionary<Udi, string> GetUrlsByUdis([FromJsonPath] Udi[] ids, string culture = null)
252+
{
253+
if (ids == null)
254+
{
255+
throw new HttpResponseException(HttpStatusCode.NotFound);
256+
}
257+
258+
if (ids.Length == 0)
259+
{
260+
return new Dictionary<Udi, string>();
261+
}
262+
263+
// TODO: PMJ 2021-09-27 - Should GetUrl(Udi) exist as an extension method on UrlProvider/IUrlProvider (in v9)
264+
string MediaOrDocumentUrl(Udi udi)
265+
{
266+
if (udi is not GuidUdi guidUdi)
267+
{
268+
return null;
269+
}
270+
271+
return guidUdi.EntityType switch
272+
{
273+
Constants.UdiEntityType.Document => UmbracoContext.UrlProvider.GetUrl(guidUdi.Guid, culture: culture ?? ClientCulture()),
274+
// NOTE: If culture is passed here we get an empty string rather than a media item URL WAT
275+
Constants.UdiEntityType.Media => UmbracoContext.UrlProvider.GetMediaUrl(guidUdi.Guid, culture: null),
276+
_ => null
277+
};
278+
}
279+
280+
return ids
281+
.Select(udi => new {
282+
Udi = udi,
283+
Url = MediaOrDocumentUrl(udi)
284+
}).ToDictionary(x => x.Udi, x => x.Url);
285+
}
286+
238287
/// <summary>
239288
/// Gets the URL of an entity
240289
/// </summary>

0 commit comments

Comments
 (0)