如何配置Sitecore,使其生成指向媒体项目的绝对链接? [英] How can I configure Sitecore so that it generates absolute links to media items?

查看:81
本文介绍了如何配置Sitecore,使其生成指向媒体项目的绝对链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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.MediaLinkPrefixmediaPrefixes似乎没有任何改变.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆