Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dmitriy 139 posts 514 karma points
    Jul 03, 2017 @ 12:42
    Dmitriy
    0

    Best way to get an absolute url for media?

    Hello.

    I've tried to get an absolute url from image with UrlWithDomain() and AbsoluteUrl() and got an exception:

    "System.NotSupportedException: AbsoluteUrl is not supported for media types"

    in both methods.

    Why does it happens? And what is the best way to get the absulute url from image when image is a value of property editor?

    Thanks.

  • Manish 372 posts 907 karma points
    Jul 03, 2017 @ 13:20
    Manish
    0

    Try something like this:

    var media = Umbraco.TypedMedia(mediaId); url = media.UrlAbsolute();

  • Dmitriy 139 posts 514 karma points
    Jul 03, 2017 @ 13:31
    Dmitriy
    0

    Nope, it does't works. The same exceptions on

    url = media.UrlAbsolute();
    
  • Dan Diplo 1467 posts 5653 karma points MVP 3x c-trib
    Jul 03, 2017 @ 13:32
    Dan Diplo
    0

    If you can't get an absolute URL from UrlAbsolute() method then a slightly hacky way is something like this - create a function that takes the relative URl and returns the absolute based on the current request context:

    public string GetAbsoluteUrl(string relativeUrl)
    {
        return new Uri(new Uri(Request.Url.GetLeftPart(UriPartial.Authority)), relativeUrl).ToString();
    }
    
  • Dmitriy 139 posts 514 karma points
    Jul 03, 2017 @ 13:56
    Dmitriy
    0

    for some reason I can't get even a relative url with myImage.Url :( It throws the same exception.

  • Dan Diplo 1467 posts 5653 karma points MVP 3x c-trib
    Jul 03, 2017 @ 14:12
    Dan Diplo
    0

    Can you post the code you are using and what version of Umbraco? Is it from a media picker or something else?

  • Dmitriy 139 posts 514 karma points
    Jul 03, 2017 @ 15:40
    Dmitriy
    0

    It was a Mediapicker2 and Umbraco 7.6

    I have rewrite the code afters solution and can't get it now :(

  • Paul Seal 395 posts 2155 karma points MVP 2x c-trib
    Jul 03, 2017 @ 14:20
    Paul Seal
    104

    Here's my approach to this:

        var domainAddress = Model.Content.AncestorOrSelf("Home").UrlAbsolute().TrimEnd('/');
        string imageUrl = Model.Content.GetPropertyValue<IPublishedContent>("headerImage").Url;
        string fullImagePath = domainAddress + imageUrl;
    
  • Dmitriy 139 posts 514 karma points
    Jul 03, 2017 @ 15:37
    Dmitriy
    0

    Thanks, Paul, it works!

    But why that problem happened, what is so specific with images so hard to get urls?

  • Lee 31 posts 79 karma points
    Oct 22, 2018 @ 22:02
    Lee
    1

    It is ridiculous that the problem still exists. It should be easy to get an absolute URL to a media item.

  • Nate 2 posts 72 karma points
    4 days ago
    Nate
    0

    I was struggling with this tonight, as of version 8.0 the following works well

    url = media.Url(mode:UrlMode.Absolute)
    

    It has been a minute since this was updated but should someone else be pulling their hair out...that worked in my use case.

Please Sign in or register to post replies

Write your reply to:

Draft