5

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

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

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.

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

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

Thanks!

4

4 に答える 4

14

拡張メソッドは、GetMediaItemUrlあなたが望むものを与えるようです。

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"));
    }
}
于 2012-10-11T17:15:12.890 に答える
3

これらは呼び出されdynamic links、通常は次のように生成できますLinkManager

Sitecore.Links.LinkManager.GetDynamicUrl(item)

..しかし、メディアリンクでこれを行う方法はわかりません(おそらく1つありますが、見つけられないようで、 on ではありませんMediaManager)が、基本的な構文は次のとおりです。

"/~/media/" + item.ID.ToShortID() + ".ashx"

于 2012-10-11T11:38:04.913 に答える
1

常にパスの代わりに ID を使用したい場合は、webconfig でこの設定を false に変更できます (次のように)。

<setting name="Media.UseItemPaths" value="false"/>`

これについて webconfig で説明されている内容は次のとおりです。

 MEDIA - USE ITEM PATHS FOR URLS
 This setting controls if item paths are used for constructing media URLs.
 If false, short ids will be used.
       Default value: true

次に、デフォルトの実装を使用できます (追加のパラメーターなし)。

Sitecore.Resources.Media.MediaManager.GetMediaUrl(item);
于 2012-10-12T19:23:59.877 に答える
0

これは私が使用するものです:

var imgField = ((Sitecore.Data.Fields.ImageField)currentItem.Fields["Icon"]);
MediaUrlOptions opt = new MediaUrlOptions();
opt.AlwaysIncludeServerUrl = true;
// Absolute Path works as well. So either use AbsolutePath or AlwaysIncludeServerUrl
opt.AbsolutePath = true;
string mediaUrl = MediaManager.GetMediaUrl(imgField.MediaItem, opt);
于 2015-05-04T16:53:12.020 に答える