Is it possible to get url with MediaManager.GetMediaUrl
that always includes the server part?
Getting the URL of a MediaItem is pretty straightforward, you pass the MediaItem reference to Sitecore. Resources. Media. MediaManager.
The default is filePath . Specifies if Sitecore lowercases the URLs it generates.
Just to bump this up, in Sitecore 7 the AlwaysIncludeServerUrl
option is also included in MediaUrlOptions
(I don't know since which version of Sitecore)
Like this:
MediaUrlOptions muo = new MediaUrlOptions();
muo.AlwaysIncludeServerUrl = true;
String url = MediaManager.GetMediaUrl((MediaItem)item, muo);
I've discovered that the following will work for producing fully qualified urls for media items:
public static string GetMediaUrlWithServer(MediaItem mediaItem, Item item = null)
{
item = item ?? Sitecore.Context.Item;
var options = new UrlOptions {AlwaysIncludeServerUrl = true, AddAspxExtension = false};
var itemUrl = LinkManager.GetItemUrl(item, options);
var mediaOptions = new MediaUrlOptions {AbsolutePath = true};
var mediaUrl = MediaManager.GetMediaUrl(mediaItem, mediaOptions);
return itemUrl + mediaUrl;
}
The urls produced will be relative to item
so you may want to supply a reference to your Home item instead of Sitecore.Context.Item
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With