Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 168 posts 588 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 373 posts 932 karma points
    Jul 03, 2017 @ 13:20
    Manish
    0

    Try something like this:

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

  • Dmitriy 168 posts 588 karma points
    Jul 03, 2017 @ 13:31
    Dmitriy
    0

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

    url = media.UrlAbsolute();
    
  • Dan Diplo 1554 posts 6205 karma points MVP 6x 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 168 posts 588 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 1554 posts 6205 karma points MVP 6x 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 168 posts 588 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 524 posts 2890 karma points MVP 7x c-trib
    Jul 03, 2017 @ 14:20
    Paul Seal
    105

    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 168 posts 588 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?

  • Roland Banguiran 7 posts 77 karma points
    Apr 30, 2020 @ 23:07
    Roland Banguiran
    0

    Hi Paul, this doesn't seemed to work on domains with culture. Url will be something like http://localhost/en-us/media/.... Is there any way to just get the root domain?

  • Roland Banguiran 7 posts 77 karma points
    Apr 30, 2020 @ 23:43
    Roland Banguiran
    0

    I use below snippet for now.

    string domainAddress = $"{Request.Url.Scheme}://{Request.Url.Authority}";
    
  • Lee 35 posts 84 karma points
    Oct 22, 2018 @ 22:02
    Lee
    2

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

  • Nate 2 posts 75 karma points
    Jan 15, 2020 @ 01:14
    Nate
    2

    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.

  • Roland Banguiran 7 posts 77 karma points
    Apr 30, 2020 @ 23:12
    Roland Banguiran
    0

    Hi Nate,

    Is there a way to get absolute Url using media.GetCropUrl(cropProfile)?

    Cheers.

  • Rowena Bellamy 17 posts 118 karma points
    Aug 03, 2020 @ 11:04
    Rowena Bellamy
    0

    Thank you for this!! Hair staying intact :)

  • Harikrishna Parmar 43 posts 262 karma points c-trib
    Feb 24, 2020 @ 03:58
    Harikrishna Parmar
    1

    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.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies