Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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>
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.
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...
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);
First one works great! Thanks douglas. Glad to learn that method in general, I'm sure I'll use it again.
Hello from 2018 :)
More obvios way:
@Url.GetAbsoluteMediaUrl(Photo) // Put your media here
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)
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted