If you use the Umbraco media picker within any of your document types to allow an editor to choose an image, when you come to render the image you may notice that the media picker only stores the id of the media item, not the Url. In your views/view models if you want to link to the image then you will need to query the Umbraco API to get the full Url. In today's post, I'm going to quickly cover how you can do that.
How To Get The Url For An Image?
To get an images Url you will need to use the Umbraco helper. Usually, you can use the snippet below:
var id = 1; var content = Umbraco.Media(id); var imagerUrl = content.Url;
If you want to use the Url in a class and not within your view/controller, you may need to instantiate the Umbraco Helper yourself (hint if you type in Umbraco.Media and Visual Studio moans, use the snippet below). You can do this, like this:
var id = 1; var umbHelper = new UmbracoHelper(UmbracoContext.Current); var content = umbHelper.Media(id); var imagerUrl = content.Url;