@@ -76,7 +76,8 @@ public void Initialize(HttpControllerSettings controllerSettings, HttpController
76
76
new ParameterSwapControllerActionSelector . ParameterSwapInfo ( "GetPath" , "id" , typeof ( int ) , typeof ( Guid ) , typeof ( Udi ) ) ,
77
77
new ParameterSwapControllerActionSelector . ParameterSwapInfo ( "GetUrlAndAnchors" , "id" , typeof ( int ) , typeof ( Guid ) , typeof ( Udi ) ) ,
78
78
new ParameterSwapControllerActionSelector . ParameterSwapInfo ( "GetById" , "id" , typeof ( int ) , typeof ( Guid ) , typeof ( Udi ) ) ,
79
- new ParameterSwapControllerActionSelector . ParameterSwapInfo ( "GetByIds" , "ids" , typeof ( int [ ] ) , typeof ( Guid [ ] ) , typeof ( Udi [ ] ) ) ) ) ;
79
+ new ParameterSwapControllerActionSelector . ParameterSwapInfo ( "GetByIds" , "ids" , typeof ( int [ ] ) , typeof ( Guid [ ] ) , typeof ( Udi [ ] ) ) ,
80
+ new ParameterSwapControllerActionSelector . ParameterSwapInfo ( "GetUrlsByIds" , "ids" , typeof ( int [ ] ) , typeof ( Guid [ ] ) , typeof ( Udi [ ] ) ) ) ) ;
80
81
}
81
82
}
82
83
@@ -236,47 +237,147 @@ public HttpResponseMessage GetUrl(Udi udi, string culture = "*")
236
237
}
237
238
238
239
/// <summary>
239
- /// Get entity URLs by UDIs
240
+ /// Get entity URLs by IDs
240
241
/// </summary>
241
- /// <param name="udis ">
242
- /// A list of UDIs to lookup items by
242
+ /// <param name="ids ">
243
+ /// A list of IDs to lookup items by
243
244
/// </param>
244
- /// <param name="culture">The culture to fetch the URL for</param>
245
+ /// <param name="type">The entity type to look for.</param>
246
+ /// <param name="culture">The culture to fetch the URL for.</param>
247
+ /// <returns>Dictionary mapping Udi -> Url</returns>
248
+ /// <remarks>
249
+ /// We allow for POST because there could be quite a lot of Ids.
250
+ /// </remarks>
251
+ [ HttpGet ]
252
+ [ HttpPost ]
253
+ public IDictionary < int , string > GetUrlsByIds ( [ FromJsonPath ] int [ ] ids , [ FromUri ] UmbracoEntityTypes type , [ FromUri ] string culture = null )
254
+ {
255
+ if ( ids == null || ! ids . Any ( ) )
256
+ {
257
+ return new Dictionary < int , string > ( ) ;
258
+ }
259
+
260
+ string MediaOrDocumentUrl ( int id )
261
+ {
262
+ switch ( type )
263
+ {
264
+ case UmbracoEntityTypes . Document :
265
+ return UmbracoContext . UrlProvider . GetUrl ( id , culture : culture ?? ClientCulture ( ) ) ;
266
+ case UmbracoEntityTypes . Media : {
267
+ var media = UmbracoContext . Media . GetById ( id ) ;
268
+ // NOTE: If culture is passed here we get an empty string rather than a media item URL.
269
+ return UmbracoContext . UrlProvider . GetMediaUrl ( media , culture : null ) ;
270
+ }
271
+ default :
272
+ return null ;
273
+ }
274
+ }
275
+
276
+ return ids
277
+ . Distinct ( )
278
+ . Select ( id => new {
279
+ Id = id ,
280
+ Url = MediaOrDocumentUrl ( id )
281
+ } ) . ToDictionary ( x => x . Id , x => x . Url ) ;
282
+ }
283
+
284
+ /// <summary>
285
+ /// Get entity URLs by IDs
286
+ /// </summary>
287
+ /// <param name="ids">
288
+ /// A list of IDs to lookup items by
289
+ /// </param>
290
+ /// <param name="type">The entity type to look for.</param>
291
+ /// <param name="culture">The culture to fetch the URL for.</param>
245
292
/// <returns>Dictionary mapping Udi -> Url</returns>
246
293
/// <remarks>
247
294
/// We allow for POST because there could be quite a lot of Ids.
248
295
/// </remarks>
249
296
[ HttpGet ]
250
297
[ HttpPost ]
251
- public IDictionary < Udi , string > GetUrlsByUdis ( [ FromJsonPath ] Udi [ ] udis , string culture = null )
298
+ public IDictionary < Guid , string > GetUrlsByIds ( [ FromJsonPath ] Guid [ ] ids , [ FromUri ] UmbracoEntityTypes type , [ FromUri ] string culture = null )
252
299
{
253
- if ( udis == null || udis . Length == 0 )
300
+ if ( ids == null || ! ids . Any ( ) )
301
+ {
302
+ return new Dictionary < Guid , string > ( ) ;
303
+ }
304
+
305
+ string MediaOrDocumentUrl ( Guid id )
306
+ {
307
+ switch ( type )
308
+ {
309
+ case UmbracoEntityTypes . Document :
310
+ return UmbracoContext . UrlProvider . GetUrl ( id , culture : culture ?? ClientCulture ( ) ) ;
311
+ case UmbracoEntityTypes . Media :
312
+ {
313
+ var media = UmbracoContext . Media . GetById ( id ) ;
314
+ // NOTE: If culture is passed here we get an empty string rather than a media item URL.
315
+ return UmbracoContext . UrlProvider . GetMediaUrl ( media , culture : null ) ;
316
+ }
317
+ default :
318
+ return null ;
319
+ }
320
+ }
321
+
322
+ return ids
323
+ . Distinct ( )
324
+ . Select ( id => new {
325
+ Id = id ,
326
+ Url = MediaOrDocumentUrl ( id )
327
+ } ) . ToDictionary ( x => x . Id , x => x . Url ) ;
328
+ }
329
+
330
+ /// <summary>
331
+ /// Get entity URLs by IDs
332
+ /// </summary>
333
+ /// <param name="ids">
334
+ /// A list of IDs to lookup items by
335
+ /// </param>
336
+ /// <param name="type">The entity type to look for.</param>
337
+ /// <param name="culture">The culture to fetch the URL for.</param>
338
+ /// <returns>Dictionary mapping Udi -> Url</returns>
339
+ /// <remarks>
340
+ /// We allow for POST because there could be quite a lot of Ids.
341
+ /// </remarks>
342
+ [ HttpGet ]
343
+ [ HttpPost ]
344
+ // NOTE: V9 - can't rename GetUrlsByUdis in v9 as it's already released, it's OK to do here as 8.18 isn't out yet.
345
+ public IDictionary < Udi , string > GetUrlsByIds ( [ FromJsonPath ] Udi [ ] ids , [ FromUri ] UmbracoEntityTypes type , [ FromUri ] string culture = null )
346
+ {
347
+ if ( ids == null || ! ids . Any ( ) )
254
348
{
255
349
return new Dictionary < Udi , string > ( ) ;
256
350
}
257
351
258
352
// TODO: PMJ 2021-09-27 - Should GetUrl(Udi) exist as an extension method on UrlProvider/IUrlProvider (in v9)
259
- string MediaOrDocumentUrl ( Udi udi )
353
+ string MediaOrDocumentUrl ( Udi id )
260
354
{
261
- if ( udi is not GuidUdi guidUdi )
355
+ if ( id is not GuidUdi guidUdi )
262
356
{
263
357
return null ;
264
358
}
265
359
266
- return guidUdi . EntityType switch
360
+ switch ( type )
267
361
{
268
- Constants . UdiEntityType . Document => UmbracoContext . UrlProvider . GetUrl ( guidUdi . Guid , culture : culture ?? ClientCulture ( ) ) ,
269
- // NOTE: If culture is passed here we get an empty string rather than a media item URL WAT
270
- Constants . UdiEntityType . Media => UmbracoContext . UrlProvider . GetMediaUrl ( guidUdi . Guid , culture : null ) ,
271
- _ => null
272
- } ;
362
+ case UmbracoEntityTypes . Document :
363
+ return UmbracoContext . UrlProvider . GetUrl ( guidUdi . Guid , culture : culture ?? ClientCulture ( ) ) ;
364
+ case UmbracoEntityTypes . Media :
365
+ {
366
+ var media = UmbracoContext . Media . GetById ( id ) ;
367
+ // NOTE: If culture is passed here we get an empty string rather than a media item URL.
368
+ return UmbracoContext . UrlProvider . GetMediaUrl ( media , culture : null ) ;
369
+ }
370
+ default :
371
+ return null ;
372
+ }
273
373
}
274
374
275
- return udis
276
- . Select ( udi => new {
277
- Udi = udi ,
278
- Url = MediaOrDocumentUrl ( udi )
279
- } ) . ToDictionary ( x => x . Udi , x => x . Url ) ;
375
+ return ids
376
+ . Distinct ( )
377
+ . Select ( id => new {
378
+ Id = id ,
379
+ Url = MediaOrDocumentUrl ( id )
380
+ } ) . ToDictionary ( x => x . Id , x => x . Url ) ;
280
381
}
281
382
282
383
/// <summary>
0 commit comments