Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Umbraco v5 how to get media file URL in razor view

I'm new to Umbraco version 5 and Razor, but I'm trying to get the path for a media file stored as a property of the current page so that I can render it as an image.

Searching Google and Stack Overflow have got me this far:

@{
    var mediaId = DynamicModel.Animation;
    var media = (TypedEntity)Umbraco.GetEntityById(mediaId);
}      

where 'Animation' is the name of the media property in my page.

How can I get to the image path of my media item?

like image 341
Rob Bird Avatar asked Mar 03 '12 16:03

Rob Bird


2 Answers

You need to use the @Umbraco.GetMediaUrl helper method. In my case:

<img src="@Umbraco.GetMediaUrl(DynamicModel.myImageProperty)" />

where myImageProperty is the name of the property in my page.

I hope this helps someone.

(OK, I found the answer to my own question, seems I was too lazy to study the sample book store site in detail which explains why there wasn't more information on the web.)

like image 120
Rob Bird Avatar answered Sep 27 '22 22:09

Rob Bird


DynamicModel is deprecated and will be removed in Umbraco 5.3, instead of it you should use CurrentPage:

<img src="@Umbraco.GetMediaUrl(CurrentPage.imageProperty)" />

like image 23
jasin_89 Avatar answered Sep 27 '22 20:09

jasin_89