Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 153 posts 538 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 153 posts 538 karma points
    Jul 03, 2017 @ 13:31
    Dmitriy
    0

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

    url = media.UrlAbsolute();
    
  • Dan Diplo 1482 posts 5723 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 153 posts 538 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 1482 posts 5723 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 153 posts 538 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 400 posts 2167 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 153 posts 538 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 35 posts 83 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 74 karma points
    Jan 15, 2020 @ 01:14
    Nate
    1

    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.

  • Harikrishna Parmar 20 posts 173 karma points
    Feb 24, 2020 @ 03:58
    Harikrishna Parmar
    0

    In Umbraco v8 :

    Here is a solution to get absolute URL.

    content.Url(string culture = null, UrlMode mode = UrlMode.Auto)

    which produces a relative-or-absolute Url depending on what's "best", but can be forced to produce absolute Urls with UrlMode.Absolute.

Please Sign in or register to post replies

Write your reply to:

Draft