4

On sitecore 8.2 inital version.

I am doing the following:

var myImageItem = (MediaItem) Sitecore.Context.Item.Fields["MyImageField"].Item;
if (myImageItem != null)
{
    viewModel.ImageAlt = myImageItem.Alt;
    var url = MediaManager.GetMediaUrl(myImageItem);
    if (!string.IsNullOrWhiteSpace(url))
    {
        viewModel.ImageUrl = url;
    }
}

The url however is always returning a 404 item not found. The logs show the following:

14008 13:40:38 WARN  Request is redirected to document not found page. Requested url: /media/94EF96BAA8214F6D9262C9FCB2137F2C.ashx, User: sitecore\admin, Website: MyWebsite

15432 13:40:58 WARN  Request is redirected to document not found page. Requested url: /media/94EF96BAA8214F6D9262C9FCB2137F2C.ashx, User: sitecore\admin, Website: MyWebsite

I have turned off mediarequest extension default of .ashx by following instructions on: https://community.sitecore.net/technical_blogs/b/sitecorejohn_blog/posts/sitecore-idiosyncrasies-media-urls

Even with this turned off .ashx media urls still being returned.

  • 1
    Have you checked if your media items are published in the web database? – Szymon Kuzniak Oct 12 '16 at 13:32
  • @SzymonKuzniak yes I have. I triple checked and they are. – Anicho Oct 12 '16 at 13:32
  • In reference to the above: can you see the media items when you're previewing on the CM? Also: if you manually change the URL to what you think it should be - do you get a result? I'm not sure if it's possible to have a media URL without a -/ (or ~/) at the start? – Owen Niblock Oct 12 '16 at 13:33
  • 1
    Sorry for obviuos question but better check :) From another direction: is /-/media/94EF96BAA8214F6D9262C9FCB2137F2C.ashx url return 404 or not? – Szymon Kuzniak Oct 12 '16 at 13:34
  • @OwenNiblock I can see on CM and can see them on the web database for the CDs – Anicho Oct 12 '16 at 13:34
6

Your problem lays in casting to MediaItem object, you should use ImageField instead.

Use this:

Sitecore.Data.Fields.ImageField myImageItem = (Sitecore.Data.Fields.ImageField)Sitecore.Context.Item.Fields["MyImageField"];

then:

Sitecore.Resources.Media.MediaManager.GetMediaUrl(myImageItem.MediaItem);
  • sorry a typo not typing but casting – Sergey Kravchenko Oct 12 '16 at 13:37
  • 1
    ahh good spot!! Let me give this a try. – Anicho Oct 12 '16 at 13:38
  • 1
    This worked yay! – Anicho Oct 12 '16 at 14:35
  • Glad I could help. – Sergey Kravchenko Oct 12 '16 at 14:37
3

Looking at the error message your media item URLs are being generated as /media. The default settings in Sitecore 8.2 are to generate with /-/media. Browse to /sitecore/admin/showconfig.aspx and check the value of Media.MediaLinkPrefix. By default it should be empty.

If the media prefix has intentionally been changed the you also need to update the following settings to add your new prefix as the trigger:

<sitecore>    
  <settings>
    <setting name="Media.MediaLinkPrefix" set:value="media" />
  </settings>

  <!-- Media handler prefixes -->
  <customHandlers>
    <handler patch:before="*[@trigger='-/media/']" trigger="media/" handler="sitecore_media.ashx" />
  </customHandlers>

  <mediaLibrary>
    <mediaPrefixes>
      <prefix value="-/media"/>
    </mediaPrefixes>
  </mediaLibrary>
</sitecore>

Note that the trigger prefix should be unique. This is why Sitecore uses the -/ part of the default prefix. If you have content which matches the prefix trigger then it will match as a media item instead, e.g. /products/media/item/ try to match as a media item. Either ensure the trigger is unique or disallow users creating items with that name.

  • 1
    <setting name="Media.MediaLinkPrefix" value=""/> it is empty in showconfix.aspx :'( – Anicho Oct 12 '16 at 13:48
  • ahh good shout, making sure those are there! – Anicho Oct 12 '16 at 13:52
  • Noticed a dirty ~/media defined under mediaprefixes node. However this didn't resolve issue :O. I changed to the default -/media – Anicho Oct 12 '16 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.