Copied to clipboard

Flag this post as spam?

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


  • Amir Khan 1009 posts 2068 karma points
    Feb 24, 2012 @ 20:47
    Amir Khan
    0

    Absolute image URL

    Hi,


    Is it possible to get the absolute URL of an image in razor? I'm trying to use the following, simply putting http://www.mysite.com in front of it doesn't work...

     

    <span class='st_pinterest' st_img="@Model.Media("headerImage", "umbracoFile")" displayText='Share'></span>

    Thanks,

    Amir

  • Douglas Ludlow 210 posts 366 karma points
    Feb 24, 2012 @ 23:01
    Douglas Ludlow
    1

    Umbraco doesn't know what the server binding is as far as I know, but you can get the domain using the HTTPContext.

    I'm just curious why hardcoding http://www.mysite.com in front of it doesn't work for you.

    <span class='st_pinterest' st_img="http://www.mysite.com@Model.Media("headerImage", "umbracoFile")" displayText='Share'></span>

    That should work just fine. But using the HTTPContext:

    dynamic image = Library.MediaById(Model.headerImage);
    string src = "http://" + HttpContext.Current.Request.Url.Host + image.umbracoFile;

    <img sre="@src" alt="" />

    ...you can get to it that way. But HTTPContext allows you to access your site's url dynamically.

  • Amir Khan 1009 posts 2068 karma points
    Feb 26, 2012 @ 20:24
    Amir Khan
    0

    Hi Douglas, I'm getting the following error with the second method you recommend:  'string' does not contain a definition for 'umbracoFile'

     

    And with the first, if I just add http://mysite.com before it, it pritns the actual code for some reason instead of the path to the image, it prints the image path fine without the prepended url...

    Any ideas?

    Thanks!

    Amir

  • Douglas Ludlow 210 posts 366 karma points
    Feb 27, 2012 @ 15:50
    Douglas Ludlow
    0

    Sorry, method 1 was untested. The way to fix that is to wrap parentheses around the razor code like so:

    <span class='st_pinterest' st_img="http://www.mysite.com@(Model.Media("headerImage", "umbracoFile"))" displayText='Share'></span>

    Using method 2, make sure you're using the dynamic keyword

    dynamic image = Library.MediaById(Model.headerImage);

     

  • Amir Khan 1009 posts 2068 karma points
    Feb 27, 2012 @ 17:20
    Amir Khan
    0

    First one works great! Thanks douglas. Glad to learn that method in general, I'm sure I'll use it again.

  • Dmitriy 153 posts 538 karma points
    Jul 20, 2018 @ 15:22
    Dmitriy
    2

    Hello from 2018 :)

    More obvios way:

    @Url.GetAbsoluteMediaUrl(Photo) // Put your media  here
    
  • Nate 2 posts 74 karma points
    Jan 15, 2020 @ 01:15
    Nate
    2

    I came across this tonight while trying to resolve an issue in Umbraco 8.x, the below code worked in my use case.

        url = media.Url(mode:UrlMode.Absolute)
    
Please Sign in or register to post replies

Write your reply to:

Draft