如何配置Sitecore,使其生成指向媒体项目的绝对链接? [英] How can I configure Sitecore so that it generates absolute links to media items?
问题描述
<sc:Image>
和<sc:FieldRenderer>
,在呈现MediaItem时,生成如下所示的html代码:
<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" />
请注意src
属性中使用的相对路径:这意味着当这样的图像在多个页面上重复使用时,浏览器必须多次获取它(例如,在 http://example.com/about-us/).在代码中生成img
标记时,可以使用以下代码段强制使用绝对网址:
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));
如何配置Sitecore以在媒体网址中强制使用斜杠?
How can I configure Sitecore to force the leading slash in media urls?
使用Reflector,我可以看到MediaOptions.AbsolutePath
控制了我想要实现的行为,但是我不知道如何为Sitecore内置控件设置它.在web.config
中设置Media.MediaLinkPrefix
或mediaPrefixes
似乎没有任何改变.
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.
推荐答案
我从事的项目是在Akamai的CDN上存储媒体资产,因此我们不得不更改媒体URL的解析方式.
I worked on a project where we stored media assets on Akamai's CDN, so we had to change how the media URLs resolved.
我们通过更改ExpandDynamicLinks()
修改了内置的LinkProvider
类.我们还修改了Sitecore.Resources.Media.MediaProvider
并更新了GetMediaUrl()
方法.
We adapted the built-in LinkProvider
class by changing ExpandDynamicLinks()
. We also adapted Sitecore.Resources.Media.MediaProvider
and updated the GetMediaUrl()
method.
这些用于处理由字段渲染器生成的图像的链接以及在Rich Text编辑器中创建的链接.
These were to handle links to images generated by field renderers and links created within a Rich Text editor.
这篇关于如何配置Sitecore,使其生成指向媒体项目的绝对链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!