开发者

Get media url including server part

开发者 https://www.devze.com 2023-03-08 11:28 出处:网络
Is it possible to get url with MediaManager.GetMediaU开发者_Go百科rl that always includes the server part?Just to bump this up, in Sitecore 7 the AlwaysIncludeServerUrl option is also included in Medi

Is it possible to get url with MediaManager.GetMediaU开发者_Go百科rl that always includes the server part?


Just to bump this up, in Sitecore 7 the AlwaysIncludeServerUrl option is also included in MediaUrlOptions (I don't know since which version of Sitecore)

Like this:

MediaUrlOptions muo = new MediaUrlOptions();
muo.AlwaysIncludeServerUrl = true;
String url = MediaManager.GetMediaUrl((MediaItem)item, muo);


I've discovered that the following will work for producing fully qualified urls for media items:

public static string GetMediaUrlWithServer(MediaItem mediaItem, Item item = null)
{
    item = item ?? Sitecore.Context.Item;
    var options = new UrlOptions {AlwaysIncludeServerUrl = true, AddAspxExtension = false};
    var itemUrl = LinkManager.GetItemUrl(item, options);
    var mediaOptions = new MediaUrlOptions {AbsolutePath = true};
    var mediaUrl = MediaManager.GetMediaUrl(mediaItem, mediaOptions);
    return itemUrl + mediaUrl;
}

The urls produced will be relative to item so you may want to supply a reference to your Home item instead of Sitecore.Context.Item


I just answered a similar question on Stack Overflow recently. I believe the answer applies to yours as well.

Short summary: there is no configuration to do this, you need to override some of the built-in methods to do this. See the above link for the exact details.


Yes, you can do that!

The correct way of setting this parameter is specifying within config file at linkManager section, where you have this anв the rest of settings regarding how you URLs will be resolved. Here's the whole section, you're interested in alwaysIncludeServerUrl parameter:

<linkManager defaultProvider="sitecore">
  <providers>
    <clear />
    <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel"
         alwaysIncludeServerUrl="true"             
         addAspxExtension="true"
         encodeNames="true"
         languageEmbedding="asNeeded"
         languageLocation="filePath"
         shortenUrls="true"
         useDisplayName="false" />
  </providers>
</linkManager>
0

精彩评论

暂无评论...
验证码 换一张
取 消