How can I configure Sitecore so that it generates absolute links to media items?
Asked Answered
C

3

8

<sc:Image> and <sc:FieldRenderer>, when rendering a MediaItem, generate html code that looks like the following:

<img src="~/media/twitter.gif" alt="Twitter" width="100" height="22" />

Notice the relative path used in src attribute: this means that when such an image is reused on multiple pages, the browser has to fetch it multiple times (e.g. on page http://example.com/ and http://example.com/about-us/). When I generate the img tag in code, I can use the following snippet to force an absolute URL:

string url = Sitecore.StringUtil.EnsurePrefix('/',
    Sitecore.Resources.Media.MediaManager.GetMediaUrl(media));

How can I configure Sitecore to force the leading slash in media urls?

Using Reflector I can see that MediaOptions.AbsolutePath controls the behaviour that I want to achieve, but I don't know how can I set it for Sitecore built-in controls. Setting Media.MediaLinkPrefix or mediaPrefixes in web.config doesn't seem to change anything.

Cis answered 12/5, 2011 at 12:16 Comment(0)
T
9

I worked on a project where we stored media assets on Akamai's CDN, so we had to change how the media URLs resolved.

We adapted the built-in LinkProvider class by changing ExpandDynamicLinks(). We also adapted Sitecore.Resources.Media.MediaProvider and updated the GetMediaUrl() method.

These were to handle links to images generated by field renderers and links created within a Rich Text editor.

Tarbox answered 12/5, 2011 at 18:21 Comment(5)
That's the way we have solved this currently, but I was hoping for a configuration solution - Reflector shows that Sitecore is able to do this, I just don't know how to enable it. But if no configuration answer shows up, I'll accept yours.Cis
I agree -- I would LOVE to see a config solution to this but I don't think there is one. * Fingers crossed *Tarbox
I haven't found a config solution either. The last 2 sites I worked on used a CDN to serve media items and we had to do something very similar.Arzola
This would be a great opportunity to create a shared source module with these new classes and then a config file to patch the default settings.Tarbox
We got response back from Sitecore - this is the solution they themselves are suggesting.Cis
C
3

There's a much easier solution to this. I may be a little off with my syntax - please feel free to correct me and I'll make edits.

MediaManager.GetItemUrl(item, new MediaUrlOptions { AbsolutePath = true });
Conant answered 3/10, 2013 at 23:31 Comment(1)
The correct syntax is MediaManager.GetMediaUrl(item, new MediaUrlOptions { AbsolutePath = true });Pampas
L
0

Create a Link to Sitecore Media Item

You will not be able to fetch the Media Item using the above LinkManager.GetItemUrl() Method. Sitecore has a separate API to fetch the Media URLs.

public string ResolveSitecoreMediaURL(Sitecore.Data.Items.Item item)
{
   MediaUrlOptions mediaOptions= new MediaUrlOptions();
   mediaOptions.AlwaysIncludeServerUrl = true;
   mediaOptions.AbsolutePath =true;
   return Sitecore.Resources.Media.MediaManager.GetMediaUrl(item,mediaOptions);
}

Like LinkManager had the URLOptions MediaManager comes with a MediaUrlOptions where you can control the absolute and relative URLs.

Lansing answered 4/6, 2016 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.