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? Oct 12, 2016 at 13:32
  • @SzymonKuzniak yes I have. I triple checked and they are.
    – Anicho
    Oct 12, 2016 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? Oct 12, 2016 at 13:33
  • 1
    Sorry for obviuos question but better check :) From another direction: is /-/media/94EF96BAA8214F6D9262C9FCB2137F2C.ashx url return 404 or not? Oct 12, 2016 at 13:34
  • @OwenNiblock I can see on CM and can see them on the web database for the CDs
    – Anicho
    Oct 12, 2016 at 13:34

2 Answers 2


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"];


  • sorry a typo not typing but casting Oct 12, 2016 at 13:37
  • 1
    ahh good spot!! Let me give this a try.
    – Anicho
    Oct 12, 2016 at 13:38
  • 1
    This worked yay!
    – Anicho
    Oct 12, 2016 at 14:35
  • Glad I could help. Oct 12, 2016 at 14:37

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:

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

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

      <prefix value="-/media"/>

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, 2016 at 13:48
  • ahh good shout, making sure those are there!
    – Anicho
    Oct 12, 2016 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, 2016 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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