Copied to clipboard

Flag this post as spam?

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


  • Phong Nguyen Dinh 26 posts 197 karma points
    Jan 18, 2017 @ 10:39
    Phong Nguyen Dinh
    0

    Hi guys, I'm newbie in umbraco and i want to ask :

    Is there any property that we can get url from Media? Take a look at my attachment:

    enter image description here

    Thank you very much. Regard, Phong.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 19, 2017 @ 22:44
    Alex Skrypnyk
    0

    Hi Phong,

    Welcome to the best community in the world!

    It's very easy to get media URL.

    Do you want to get media URL in Razor code?

    Example how to do it:

    var mediaItem = Umbraco.TypedMedia("imageID"); 
    var mediaUrl = mediaItem.Url;
    

    Thanks,

    Alex

  • Jeff Subat 28 posts 159 karma points
    Jan 20, 2017 @ 00:32
    Jeff Subat
    0

    To augment Alex's comments, Phong, here's a little helper function I use that saves me a line of two of code in the view, and also guards against null pointers.

    Can't take credit for this, as I think I found something similar to it elsewhere in this forum :)

        public static string GetMediaUrl(string mediaId, UmbracoHelper uHelper)
                {
                    var itemUrl = "";
                    var mediaItem = uHelper.TypedMedia(mediaId);
    
                    if (mediaItem != null)
                    {
                        itemUrl = GetItemUrl(mediaItem);
                    }
    
                    return itemUrl;
                }
    
    // Used by both GetMediaUrl (above) and GetContentUrl (below)
    public static string GetItemUrl(IPublishedContent content)
            {
                string url = content != null ? content.Url : "";
    
                return url;
            }
    // Like GetMediaUrl, but uses TypedContent for document types
    public static string GetContentUrl(string contentId, UmbracoHelper uHelper)
            {
                var itemUrl = "";
                var contentItem = uHelper.TypedContent(contentId);
    
                if (contentItem != null)
                {
                    itemUrl = GetItemUrl(contentItem);
                }
    
                return itemUrl;
            }
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 20, 2017 @ 08:42
    Alex Skrypnyk
    0

    Hi Jeff,

    Thank you for code example, but can you share "GetItemUrl" method also?

    Because this code isn't all

    Thanks,

    Alex

  • Jeff Subat 28 posts 159 karma points
    Jan 20, 2017 @ 14:18
    Jeff Subat
    0

    Thanks for pointing that out. It looks like Phong has found the solution, but I edited my answer just to be thorough.

  • Phong Nguyen Dinh 26 posts 197 karma points
    Jan 20, 2017 @ 11:07
    Phong Nguyen Dinh
    101

    Hi Alex Skrypnyk and Jeff Subat,

    Thank you very much for your replies, i really appreciate your help in resolving my question.

    Sorry for my late reply. I figured it out myself tomorrow, this is the code i used and It worked like a charm:

    var metaImage = CurrentNode.GetPropertyValue<string>("bannerImage");
    var imageUrl = "http://" + HttpContext.Current.Request.Url.Host + Umbraco.TypedMedia(int.Parse(metaImage)).GetCropUrl("Crop Alias");
    

    I think Alex Skrypnyk's way is great but my question's mean i want to get the url include the domain name of the site. (Sorry for my miss-information question, my english is not good enough).

    Anyway, thank you guys again, i love this community.

    Thanks,

    Phong.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jan 20, 2017 @ 11:44
    Alex Skrypnyk
    0

    Hi Phong,

    Thank you very much for sharing the solution!!!

    Have a nice day!!!

    Thanks,

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft