How can I configure Sitecore so that it generates absolute links to media items?

后端 未结 3 548
北荒
北荒 2020-12-10 16:13

and , when rendering a MediaItem, generate html code that looks like the following:



        
相关标签:
3条回答
  • 2020-12-10 16:21

    There's a much easier solution to this. I may be a little off with my syntax - please feel free to correct me and I'll make edits.

    MediaManager.GetItemUrl(item, new MediaUrlOptions { AbsolutePath = true });
    
    0 讨论(0)
  • 2020-12-10 16:29

    I worked on a project where we stored media assets on Akamai's CDN, so we had to change how the media URLs resolved.

    We adapted the built-in LinkProvider class by changing ExpandDynamicLinks(). We also adapted Sitecore.Resources.Media.MediaProvider and updated the GetMediaUrl() method.

    These were to handle links to images generated by field renderers and links created within a Rich Text editor.

    0 讨论(0)
  • 2020-12-10 16:40

    Create a Link to Sitecore Media Item

    You will not be able to fetch the Media Item using the above LinkManager.GetItemUrl() Method. Sitecore has a separate API to fetch the Media URLs.

    public string ResolveSitecoreMediaURL(Sitecore.Data.Items.Item item)
    {
       MediaUrlOptions mediaOptions= new MediaUrlOptions();
       mediaOptions.AlwaysIncludeServerUrl = true;
       mediaOptions.AbsolutePath =true;
       return Sitecore.Resources.Media.MediaManager.GetMediaUrl(item,mediaOptions);
    }
    

    Like LinkManager had the URLOptions MediaManager comes with a MediaUrlOptions where you can control the absolute and relative URLs.

    0 讨论(0)
提交回复
热议问题