Images Broken after upgrading to Sitecore 7.5 Initial Release

28 October 2014

TL;DR

If you get 404 errors on images in Sitecore following an upgrade to 7.5 and you are using the web.config setting encodeNameReplacements and some of your media item names include the replaceWith character, then you should rename the broken media items avoiding the replaceWith characters.

Update: there is a Sitecore KB article which describes this problem and the solution: https://kb.sitecore.net/articles/998758

Broken Images in Sitecore 7.5 (The long-winded version)

After upgrading a project from Sitecore 7.1 to 7.5 I discovered that some of the images were returning 404 errors. After a little playing around, I discovered that only images which have a hyphen in them would get broken. This was because we’re using the <encodeNameReplacements> setting in our web.config to dynamically replace spaces in item names (and hence Media Item names) with hyphens (for improved readability and SEO). After some debugging of the Sitecore.Kernel assembly using dotPeek I tracked the issue down to the Sitecore.Resources.Media.MediaRequest class. This class has a method called GetMediaPath (GetMediaPath(string localPath)) which takes the request Url and generates the corresponding item path. Prior to Sitecore 7.5 this worked fine, however looking at the 7.5 version of the method, there is a call to Sitecore.MainUtil.DecodeName (which uses the list of name replacements from the config) this means that if an item has a name using one of these name replacements (i.e. some-media-item) the reverse character substitution is performed on the name, so this name gets changed to “some media item” which doesn’t exist in Sitecore so we get a 404 error for the images.

Attempted Solution

I found a setting in the web.config called <requestParser> (sitecore/settings/mediaLibrary/requestParser) which gave me hope that I would be able to create a new class inheriting from the existing Sitecore.Resources.Media.MediaRequest class, simply overriding the GetMediaPath method, but it seems that this is not possible since the original class implements the IPrototype<TObjectType> interface - the overriding class has to inherit IPrototype<Sitecore.Resources.Media.MediaRequest> and hence has to implement the clone method which has to return an instance of the original Sitecore MediaRequest class. Whenever the calling class (MediaConfig) instantiates your MediaRequest class it calls the Clone method which means your new class can never get used :-(

The take-away from this is that if you are trying to override a Sitecore class which implements IPrototype, you’re probably trying to do something that Sitecore didn’t intend you to be able to do, so you're probably best to avoid it!

Conclusion

  • To fix broken images, rename the offending media items. If you have a lot of media items to modify, consider using Sitecore Powershell Extensions
  • If you are trying to override a Sitecore class to modify behaviour, watch out for the IPrototype interface - if you see this you’re probably not meant to be overriding this!
Tags: Media Library
comments powered by Disqus