Copied to clipboard

Flag this post as spam?

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


  • George Phillipson 108 posts 287 karma points
    Feb 28, 2019 @ 21:45
    George Phillipson
    0

    Get image url from MacroPartialView

    Hi I used to be able to get image url from macro with:

    var imgMain = Model.MacroParameters["image"];
        int imgWidth = Convert.ToInt32(Model.MacroParameters["imageWidth"]);
        var mediaItemOne = Umbraco.TypedMedia(imgMain);
        <img src='@mediaItemOne.Url.GetCropUrl(width:imageWidth)' alt=''/ >
    

    What is the new way in Umbraco 8 as I get the following error now and I cannot find any documentation on how to do it:

    UmbracoHelper' does not contain a definition for 'TypedMedia' and no accessible extension method 'TypedMedia' accepting a first argument of type 'UmbracoHelper' could be found (are you missing a using directive or an assembly reference?)

    OK, if anyone comes across this post, this is how it works for me.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @using Umbraco.Web;
    @using Umbraco.Web.Composing
    @{
        var img = Model.MacroParameters["image"].ToString();
        int imageWidth = Convert.ToInt32(Model.MacroParameters["imageWidth"]);
        var udi = Udi.Parse(img);
        var imgUrl = Current.UmbracoHelper.ContentQuery.Media(udi).Url;
        <img src='@imgUrl.GetCropUrl(width:imageWidth)' alt='' />
    }
    
  • Ole Martin Bakke 112 posts 624 karma points
    Mar 04, 2019 @ 14:28
    Ole Martin Bakke
    1

    I think Umbraco.TypedMedia is renamed to just Umbraco.Media. So you can probably do something like.

    var media = Umbraco.Media(Model.GetParameterValue<string>("image"));
    

    This will give you a IPublishedContent object, and you can then do:

    <img src="@media.GetCropUrl(imageWidth) alt=""/>
    
  • Debasish Gracias 32 posts 163 karma points
    Sep 03, 2021 @ 07:46
    Debasish Gracias
    0

    Im using something similar but I cant get it to work. Here is my code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var Text = Model.MacroParameters["text"].ToString();
        var Image = Model.MacroParameters["image"].ToString();
        var udi = Udi.Parse(Image);
        var media = Umbraco.Media(udi);
    }
    
    <section class="section animPublications wow fadeIn imagewithtext">
        <div class="aligncenter">
            <figure class="imageanimate wow zoomIn" data-wow-offset="200">
                    <img src="@media.Url()" alt="icon" />
                <figcaption>@Text</figcaption>
            </figure>
        </div>
    </section>
    

    Im getting the below error:

    System.NullReferenceException: Object reference not set to an instance of an object. at Umbraco.Web.PublishedContentExtensions.Url(IPublishedContent content, String culture, UrlMode mode) in D:\a\1\s\src\Umbraco.Web\PublishedContentExtensions.cs:line 1436 at ASP.PageViewsMacroPartialsAnimationsImageWithTextAnimationMacrocshtml.Execute()

    Any help?

  • Damian Chambers 23 posts 87 karma points
    Jul 17, 2023 @ 10:53
    Damian Chambers
    0

    I think you are using Url() instead of GetCropUrl(). So, instead of using

     <img src="@media.Url()" alt="icon" />
    

    You should use:

     <img src="@media.GetCropUrl()" alt="icon" />
    
Please Sign in or register to post replies

Write your reply to:

Draft