Copied to clipboard

Flag this post as spam?

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


  • NK 8 posts 78 karma points
    May 09, 2018 @ 17:25
    NK
    0

    How to Generate Full Media Picker URL?

    How can I change this from showing the Media Picker path to the full URL?

    @Umbraco.Media(Model.Content.GetPropertyValue("postImage").ToString()).Url
    

    That outputs /media/1038/image.jpg, but I need to instead output https://www.example.com/media/1038/image.jpg

    I have tried editing the original code but can't figure it out. Do I need to somehow use insertBefore?

    This is all code I am looking to add to a template file. Please be detailed with your instructions; I am still getting familiar with Umbraco.

    Second, I would like to have a fallback image if #postImage doesn't exist (so the page doesn't break). Any help will be much appreciated.

  • Rune Hem Strand 147 posts 911 karma points hq c-trib
    May 09, 2018 @ 18:36
    Rune Hem Strand
    0

    Hi NK

    You can get the full url by using GetAbsoluteMediaUrl on the Url helper, like so:

    @{
        var mediaItem = Umbraco.Media(Model.Content.GetPropertyValue("postImage");
    }
    
    @Url.GetAbsoluteMediaUrl(mediaItem)
    

    That should do the trick ☺️

    All the best
    Rune

  • NK 8 posts 78 karma points
    May 09, 2018 @ 23:26
    NK
    0

    Hi Rune,

    This will be a dumb question, but what is the URL helper? And how do I implement the code you provided? All customizations I have done thus far have been limited to template editing. I have not had to define variables/etc.

    I tried adding your variable to the top @{ } section, then using @Url.GetAbsoluteMediaUrl(mediaItem) within the template, but there were parsing errors. It says 'System.Web.Mvc.UrlHelper' has no applicable method named 'GetAbsoluteMediaUrl'

    I believe your var declaration is missing a closing parenthesis, by the way.

    Thank you.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    May 10, 2018 @ 07:50
    Dennis Aaen
    0

    Hi NK

    You can full url by using this little snippet of code below.

    @{
         var domainAddress = Model.Content.Site().UrlAbsolute().TrimEnd('/');
         string imageUrl = Model.Content.GetPropertyValue<IPublishedContent>("postImage").Url;
         string fullImagePath = domainAddress + imageUrl;
    
         @fullImagePath
    
    }
    

    Best,

    /Dennis

  • NK 8 posts 78 karma points
    May 11, 2018 @ 20:23
    NK
    0

    That did it. Thank you. Quick follow-up question:

    What is the easiest way, then, to set up an if/else that uses a default image URL if #postImage is null? Don't want the page to break when an image doesn't exist.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    May 12, 2018 @ 08:18
    Dennis Aaen
    0

    Hi NK,

    You can do it like this.

       @{
         var domainAddress = Model.Content.Site().UrlAbsolute().TrimEnd('/');
            if(Model.Content.HasValue("postImage")){ 
                string imageUrl = Model.Content.GetPropertyValue<IPublishedContent>("postImage").Url;
                string fullImagePath = domainAddress + imageUrl;
                    <img src="@fullImagePath" alt=""/>
    
            }else{ 
                <img src="@domainAddress/media/1016/14272036539_469ca21d5c_h.jpg" alt=""/>
            }
    
        }
    

    Remember to change the default image URL if postImage is null

    Hope this helps,

    /Dennis

  • Nathan Woulfe 447 posts 1664 karma points MVP 5x hq c-trib
    May 12, 2018 @ 09:09
    Nathan Woulfe
    0

    Other option, although it adds more code, is to go down the MVC track and get the image in your controller, setting a default value if nothing is defined. Always good to separate concerns where it makes sense to do so.

    That way, in your view, you can simply render the image from the your model with full confidence that the image will always have a value.

  • pranjal 75 posts 188 karma points
    May 10, 2018 @ 07:26
    pranjal
    0

    I am not getting your Question you want to pick complete media files and for that you want a Query than it is pretty simple

    var mediaFiles = Umbraco.media(InsertyourMediaFileID(ex-1181)).Children();
    

    This will get all media files list from media section.

  • NK 8 posts 78 karma points
    May 11, 2018 @ 20:17
    NK
    0

    Not what I asked, but thanks.

Please Sign in or register to post replies

Write your reply to:

Draft