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.

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.

Proudly sponsored by

Moriyama

  • Moriyama build, support and deploy Umbraco, Azure and ASP.NET websites and applications.
AppVeyor

  • CI/CD service for Windows, Linux and macOS
  • Build, test, deploy your apps faster, on any platform.
elmah.io

  • elmah.io is the easy error logging and uptime monitoring service for .NET.
  • Take back control of your errors with support for all .NET web and logging frameworks.
uSync Complete

  • uSync.Complete gives you all the uSync packages, allowing you to completely control how your Umbraco settings, content and media is stored, transferred and managed across all your Umbraco Installations.
uSkinned

  • More than a theme for Umbraco CMS, take full control of your content and design with a feature-rich, award-nominated & content editor focused website platform.
UmbHost

  • Affordable, Geo-Redundant, Umbraco hosting which gives back to the community by sponsoring an Umbraco Open Source Developer with each hosting package sold.