如何在Sitecore中生成ID而不是路径的媒体项目链接 [英] How to generate media item link with id instead of path in Sitecore

查看:148
本文介绍了如何在Sitecore中生成ID而不是路径的媒体项目链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在Sitecore中使用ID而不是项目路径生成链接吗?

Anyone knows how to generate links in sitecore with ID instead of item path?

如果您通过API使用GetMediaUrl方法,则可以获取以下URL:

If you use GetMediaUrl method from the API, I can get this URL:

/〜/media/Images/Archive/content/News and Events/News_and_Events_Level2/20070419162739/iwhiz3.jpg

这种方法的问题是,如果有人更改了媒体项名称,将其删除或删除了,则以上链接将中断.

The problem with this approach is that if someone changes the media item name, removes it somewhere or deletes it, the above link will break.

我注意到,如果我从富文本编辑器插入媒体链接,则会得到如下链接:

I notice if I insert a media link from rich text editor, I get the link as below:

/~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx

第二个链接更好,因为它使用的是项目ID,因此,如果实际的媒体项目被重命名,删除或删除,那么所有相关链接也会被更新.最重要的是,当Sitecore呈现页面时,它将实际上转换上面的链接并显示项目路径,以便可读.

The second link is better because it's using the item id, so if the actual media item is renamed, removed, or deleted, all related links will be updated too. On top of that, when Sitecore renders the page, it will actually convert the above link and display the item path so it's readable.

我正在使用Sitecore 6.5,目前正在进行内容迁移,因此我需要确保所有内部链接均已正确更新.

I'm using Sitecore 6.5 and currently doing content migration so I need to make sure all internal links are updated properly.

我可以知道是否存在使用sitecore API生成第二个链接的方法吗?

May I know if there is a method to generate the second link by using sitecore API?

谢谢!

推荐答案

GetMediaItemUrl扩展方法似乎可以为您提供所需的内容.

The GetMediaItemUrl extension method seems to give you what you want.

public static class ItemExtensions
{
    public static string GetMediaItemUrl(this Item item)
    {
        var mediaUrlOptions = new MediaUrlOptions() { UseItemPath = false, AbsolutePath = true };
        return Sitecore.Resources.Media.MediaManager.GetMediaUrl(item, mediaUrlOptions);
    }
}

[TestFixture]
public class when_using_items_extensions
{
    [Test]
    public void a_url_based_on_media_item_id_can_be_generated()
    {
        // Arrange
        Database db = global::Sitecore.Configuration.Factory.GetDatabase("master");
        Item item = db.GetItem("/sitecore/media library/Images/MyImage");

        // Act
        var mediaUrl = item.GetMediaItemUrl();

        // Assert
        Assert.That(mediaUrl, Is.EqualTo("/~/media/17A1341ABEEC46788F2159843DCEAB03.ashx"));
    }
}

这篇关于如何在Sitecore中生成ID而不是路径的媒体项目链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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