Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...