Add EntityController GetUrlsByUdis
Enables loading multiple URLs in a single request for Media & Documents
This commit is contained in:
@@ -235,6 +235,55 @@ namespace Umbraco.Web.Editors
|
||||
return GetUrl(intId.Result, entityType, culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get entity URLs by UDIs
|
||||
/// </summary>
|
||||
/// <param name="ids">
|
||||
/// A list of UDIs to lookup items by
|
||||
/// </param>
|
||||
/// <param name="culture">The culture to fetch the URL for</param>
|
||||
/// <returns>Dictionary mapping Udi -> Url</returns>
|
||||
/// <remarks>
|
||||
/// We allow for POST because there could be quite a lot of Ids.
|
||||
/// </remarks>
|
||||
[HttpGet]
|
||||
[HttpPost]
|
||||
public IDictionary<Udi, string> GetUrlsByUdis([FromJsonPath] Udi[] ids, string culture = null)
|
||||
{
|
||||
if (ids == null)
|
||||
{
|
||||
throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
if (ids.Length == 0)
|
||||
{
|
||||
return new Dictionary<Udi, string>();
|
||||
}
|
||||
|
||||
// TODO: PMJ 2021-09-27 - Should GetUrl(Udi) exist as an extension method on UrlProvider/IUrlProvider (in v9)
|
||||
string MediaOrDocumentUrl(Udi udi)
|
||||
{
|
||||
if (udi is not GuidUdi guidUdi)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return guidUdi.EntityType switch
|
||||
{
|
||||
Constants.UdiEntityType.Document => UmbracoContext.UrlProvider.GetUrl(guidUdi.Guid, culture: culture ?? ClientCulture()),
|
||||
// NOTE: If culture is passed here we get an empty string rather than a media item URL WAT
|
||||
Constants.UdiEntityType.Media => UmbracoContext.UrlProvider.GetMediaUrl(guidUdi.Guid, culture: null),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
return ids
|
||||
.Select(udi => new {
|
||||
Udi = udi,
|
||||
Url = MediaOrDocumentUrl(udi)
|
||||
}).ToDictionary(x => x.Udi, x => x.Url);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the URL of an entity
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user