Karol Karol - 1 year ago 97
C# Question

How can I make sitecore showing an image (or other resources)

I installed clean Sitecore 6.6 and enabled MVC support using this guide. So my environment is Sitecore 6.6, ASP .NET MVC 3 and Razor, Microsoft SQL Server Express 2012, IIS 7.5 and I'm using Microsoft Visual Studio 2012 Express for Web. I have following code:

@Model.PageItem.Fields["Title"];<br />
@Model.PageItem.Fields["Image"].GetValue(true, true);<br />
@Model.PageItem.Fields["Text"];<br />
Sitecore.Data.Items.MediaItem item = Model.PageItem.Fields["Image"].Item;
@Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl(item));<br />

Result is simple:

<image mediaid="{4DFD3ABC-0BC0-41D2-BD38-705946A1368A}" mediapath="/Images/xbox" src="~/media/4DFD3ABC0BC041D2BD38705946A1368A.ashx" />
<p>Welcome to Sitecore</p>

When I navigate to the path specified in the last line I get following error:

HTTP Error 404.0 - Not Found
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

I tried few things, e.g.:

  1. Media.UseItemPaths in Web.config changed to trye (or false) - nothing works...

  2. Media.RequestExtension in Web.config set to empty (ashx is default)

  3. I have added ashx to Allowed extensions in Web.config (because if I cant have normal extensions, I at least want to have working ashx link)

  4. I have added ashx to IIS 7.5 -> Request Filtering -> File name extensions

Of course after each change (just in case) I restarted server and cleared browser's cache (actually, after few requests I have disabled cache for chrome).

I was looking for solution on sdn.sitecore.net with no luck. Actually I've spent more than 3 hrs so far looking for solution and can't figure out what is going wrong... Any help or suggestions appreciated!

Answer Source

The Field method of the Sitecore.Mvc.Helpers.SitecoreHelper class will allow you to output an Image field.

Here's an example View Rendering that outputs your three fields:

@using Sitecore.Mvc.Presentation
@using Sitecore.Mvc
@model RenderingModel

@Html.Sitecore().Field("Title")<br />
@Html.Sitecore().Field("Image")<br />
@Html.Sitecore().Field("Text")<br />

John West has blogged extensively on Sitecore MVC, you might like to look at his About MVC Helpers with the Sitecore ASP.NET CMS post.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download