UPDATED 08-Jun-2017

In Umbraco v7.6 and above, there is now a new thing called UDI. Find out more about UDI here.

So instead of your media item having a numeric id, by default it will have an id like this: umb://media/39d3ac707d634953ab52642d5037855c

Here is how you get the media url when the id is one of the new type:

string imageUrl = Model.Content.GetPropertyValue<IPublishedContent>("headerImage").Url;

7.5x and below

When using the media picker in Umbraco, it stores the id of the media item.

This post shows you how to get the file path of the item so you can use it for the src of an image or as a link to a document.

Here is how to do it inside a view or partial view:

UmbracoHelper uHelper = new UmbracoHelper(UmbracoContext.Current);
string mediaUrl = "";
if (CurrentPage.HasValue(propertyName))
{
    var mediaItem = uHelper.Media(CurrentPage.propertyName.ToString());
    mediaUrl = mediaItem.umbracoFile;
}
return mediaUrl;

Here is how to do it from a method in a class somewhere:

using Umbraco.Web;

public static string GetMediaUrlFromPropertyName(dynamic contentItem, string propertyName)
{
    UmbracoHelper uHelper = new UmbracoHelper(UmbracoContext.Current);
    string mediaUrl = "";
    if (contentItem.HasValue(propertyName))
    {
        var mediaItem = uHelper.Media(contentItem.GetPropertyValue(propertyName));
        mediaUrl = mediaItem.umbracoFile;
    }
    return mediaUrl;
}

I hope you found this useful. Let me know if you need any further help with this.

Want to thank me?

If I've helped you out and you want to thank me, why not buy me a coffee?

About the author

Paul Seal

Umbraco MVP and .NET Web Developer from Derby (UK) who specialises in building Content Management System (CMS) websites using MVC with Umbraco as a framework. Paul is passionate about web development and programming as a whole. Apart from when he's with his wife and son, if he's not writing code, he's thinking about it or listening to a podcast about it.

Related Posts

How to fix corrupt accented characters in Excel for a C# export as CSV

This post will help you fix the issue where you csv export has corrupt characters when opening it in…

Read Post

How to create a dynamic image from multiple images using ImageProcessor

This post shows you how I created a handler to create dynamic header images for my umbraco website u…

Read Post

How to send Slack messages programmatically using C#

In this post I show you how you can send Slack messages programmatically using C#

Read Post

How to fix the SVG 404 error when using Umbraco and Azure Blob Storage

In this post I show you how to solve the SVG 404 error when working with Umbraco and Azure Blob Stor…

Read Post