Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<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.

like image 949
skolima Avatar asked May 12 '11 12:05

skolima


2 Answers

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.

like image 199
Mark Ursino Avatar answered Oct 24 '22 16:10

Mark Ursino


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 });
like image 25
Teeknow Avatar answered Oct 24 '22 17:10

Teeknow