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();
}
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?
Best way to get an absolute url for media?
Hello.
I've tried to get an absolute url from image with
UrlWithDomain()
andAbsoluteUrl()
and got an exception: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.
Try something like this:
var media = Umbraco.TypedMedia(mediaId); url = media.UrlAbsolute();
Nope, it does't works. The same exceptions on
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:
for some reason I can't get even a relative url with
myImage.Url
:( It throws the same exception.Can you post the code you are using and what version of Umbraco? Is it from a media picker or something else?
It was a Mediapicker2 and Umbraco 7.6
I have rewrite the code afters solution and can't get it now :(
Here's my approach to this:
Thanks, Paul, it works!
But why that problem happened, what is so specific with images so hard to get urls?
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?I use below snippet for now.
It is ridiculous that the problem still exists. It should be easy to get an absolute URL to a media item.
I was struggling with this tonight, as of version 8.0 the following works well
It has been a minute since this was updated but should someone else be pulling their hair out...that worked in my use case.
Hi Nate,
Is there a way to get absolute Url using
media.GetCropUrl(cropProfile)
?Cheers.
Thank you for this!! Hair staying intact :)
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.
is working on a reply...