【问题标题】:How to generate media item link with id instead of path in Sitecore如何在 Sitecore 中使用 id 而不是路径生成媒体项目链接
【发布时间】:2012-10-02 01:12:55
【问题描述】:

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

如果你使用 API 中的 GetMediaUrl 方法,我可以得到这个 URL:

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

这种方法的问题在于,如果有人更改媒体项目名称、将其从某处删除或删除,上述链接将中断。

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

/~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx

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

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

请问有没有使用sitecore API 生成第二个链接的方法?

谢谢!

【问题讨论】:

    标签: sitecore sitecore6 sitecore-media-library


    【解决方案1】:

    这些称为dynamic links,您通常可以使用LinkManager 生成它们,例如:

    Sitecore.Links.LinkManager.GetDynamicUrl(item)

    .. 但我不确定使用媒体链接执行此操作的方法(可能有一个,但我似乎找不到它,它不在 MediaManager 上),但基本语法是:

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

    【讨论】:

      【解决方案2】:

      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"));
          }
      }
      

      【讨论】:

      • 这可行,但我必须将绝对路径设置为 false 否则链接将显示为 abc.com/http://abc.com/~/media/Images/Archive/content/News 和 Events/News_and_Events_Level2/20070419162739/iwhiz3.jpg 这是我的 url 选项:var URLOptions = new MediaUrlOptions { AbsolutePath = false, UseItemPath = false, AlwaysIncludeServerUrl = false, IncludeExtension = true };
      【解决方案3】:

      如果您总是想使用 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);
      

      【讨论】:

        【解决方案4】:

        这是我用的:

        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);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多