Copied to clipboard

Flag this post as spam?

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


  • Gert 81 posts 288 karma points
    Dec 19, 2014 @ 10:57
    Gert
    0

    Is there an equivalent for Umbraco.NiceUrlWithDomain() for media

    I was wondering if there exists an equivalent method for Umbraco.NiceUrlWithDomain( pageId ) to resolve the absolute url for media files?

    Thanks! Gert.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 19, 2014 @ 12:43
    Alex Skrypnyk
    0

    Hi Gert,

    Try something like this:

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

    Thanks

  • Gert 81 posts 288 karma points
    Dec 19, 2014 @ 13:37
    Gert
    0

    Hi Alex,

    The line media.UrlAbsolute() throws the following exception:

    'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'UrlAbsolute'

    I am using the following code in a macro partial:

    if ( CurrentPage.HasValue(PropertyAliasNames.OpenGraphImage) )
    {
        var media = Umbraco.TypedMedia(CurrentPage.OpenGraphImage.Id);
        imageUrl = media.UrlAbsolute();
        //imageUrl = VirtualPathUtility.ToAbsolute( "~/" + Umbraco.Media( CurrentPage.OpenGraphImage.Id ).Url );
    }

    The line in comment gives me the relative path, at least on my local host machine. 

    Regards,

    Gert.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 19, 2014 @ 13:44
    Dennis Aaen
    0

    Hi Gert,

    Perhaps this post http://stackoverflow.com/questions/6960105/how-do-i-get-the-absolute-media-file-path-in-umbraco-using-razor from StackOverflow can help you finding a solution to get absolute url for your images.

    Hope this can help you a step further.

    /Dennis

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 19, 2014 @ 13:49
    Alex Skrypnyk
    0

    Hi Dennis,

    Yes, your are right, but this is not Umbraco solution, .UrlAbsolute() should work at Umbraco 7.1 - 7.2. Because media.NiceUrlWithDomain() is obsolete.

    Thanks, Alexander

  • Gert 81 posts 288 karma points
    Dec 19, 2014 @ 13:53
    Gert
    0

    Hi Alexander,

    I am using Umbraco 7.2.1, then why media.UrlAbsolute() throws the exception?

    'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for'UrlAbsolute'

    Thanks!

     

    Regards,

    Gert.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 19, 2014 @ 14:02
    Dennis Aaen
    1

    Hi Gert,

    The issue here could be that you´re mixing the dynamic and strongly typed razor together.

    What if you do it like this:

    if (CurrentPage.HasValue(PropertyAliasNames.OpenGraphImage) )
    {
        var media = Umbraco.Media(CurrentPage.OpenGraphImage.Id);
        imageUrl = media.UrlAbsolute();
        //imageUrl = VirtualPathUtility.ToAbsolute( "~/" + Umbraco.Media(CurrentPage.OpenGraphImage.Id ).Url );
    }

    Be aware that I have changed the Umbraco.TypedMedia to just Umbraco.Media which is the dynamic representation.

    Hope this helps,

    /Dennis

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Dec 19, 2014 @ 14:09
    Alex Skrypnyk
    0

    I have the same crash, looks like it's 'AbsoluteUrl is not supported for media types.'. So now we can only use something like told Dennis, and maybe it will work in future releases of Umbraco

    Thanks,

    Alexander

  • Gert 81 posts 288 karma points
    Dec 19, 2014 @ 14:38
    Gert
    1

    Hi Dennis,

    Same exception.

    Regards,

    Gert.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Dec 19, 2014 @ 14:50
    Dennis Aaen
    102

    Hi Gert,

    A low key solution could perhaps be this:

    @if (CurrentPage.HasValue(PropertyAliasNames.OpenGraphImage) ){
        string domain = "http://" + HttpContext.Current.Request.Url.Host;
        var media = Umbraco.Media(CurrentPage.OpenGraphImage.Id);
        <img src="@[email protected]" alt="@media.Name"/>
    }

    Hope this could be a solution for you.

    /Dennis

  • Gert 81 posts 288 karma points
    Dec 19, 2014 @ 16:09
    Gert
    0

    Hi Dennis, that will do the trick! Thanks!

    Regards,

    Gert.

  • Bojan 16 posts 131 karma points
    Sep 08, 2016 @ 18:03
    Bojan
    2

    Hi to all,

    Is there any better solution for Media Absolute URLs today?

    Cheers, Bojan

Please Sign in or register to post replies

Write your reply to:

Draft