Sitecore 5.3 and ImageURI

If you assign the path of a media item to a Image.ImageUrl in code behind it will not find the image unless you prefix the path with a /, see the example code below:

// ensure that the url has a / before any ~
string spotItemURL = myImageField.Path;
  spotItemURL = /" + spotItemURL;

If you do not make the above check the image will be renders in HTML as follows:

<img src="~/media/60DAF58AD915497A893315F10CCEBD94.ashx" />

And both IE & Firefox cannot find the image, if you prefix the url with a /, using the code above the output will be as follows:

<img src="/~/media/60DAF58AD915497A893315F10CCEBD94.ashx">

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 5 and tagged , , . Bookmark the permalink.

One Response to Sitecore 5.3 and ImageURI

  1. Alex Shyba says:

    Hi Brian,
    Another way to approach this is to use the favorite StringUtil class :-)
    Sitecore.StringUtil.EnsurePrefix(‘/’, spotItemURL);

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.