Copied to clipboard

Flag this post as spam?

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


  • Laurence Gillian 600 posts 1219 karma points
    Sep 26, 2016 @ 09:26
    Laurence Gillian
    0

    Accessing Helper Methods

    Hey!

    I'm installing the Azure CDN Toolkit for the first time, and I'm struggling to gain access the Helper methods inside my strongly typed object model.

    I can see the extensions are contained inside "Our.Umbraco.AzureCDNToolkit.UrlHelperRenderExtensions" however, I can't figure out how to get this context into my C# code.

    namespace client.project.Logic.Model.Media
    {
        public class Image : BaseContentType
        {
    
            public Image(IPublishedContent content)
                : base(content)
            {
            }
    
            public string Url
            {
                get
                {
                    return this.GetCropUrl(imageCropMode: ImageCropMode.Max, width: 1200);
                }
            }
        }
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 26, 2016 @ 10:04
    Jeavon Leopold
    1

    What do you have in your BaseContentType?

  • Laurence Gillian 600 posts 1219 karma points
    Sep 26, 2016 @ 10:15
    Laurence Gillian
    0

    Here we go, a few helpers and a way of bringing back Typed Media/Content.

    using System;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    
    namespace project.client.Logic.Model.Content
    {
        public abstract class BaseContentType : PublishedContentModel
        {
    
                private UmbracoHelper _umbracoHelper;
    
                protected BaseContentType(IPublishedContent content)
                        : base(content)
                {
                        _umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
                }
    
    
                protected T GetProperty<T>(string propertyAlias)
                {
                        return this.GetPropertyValue<T>(propertyAlias);
                }
    
                protected bool PropertyExists(string propertyAlias)
                {
                        return this.HasProperty(propertyAlias);
                }
    
                protected IPublishedContent GetMediaByAlias(string alias)
                {
                        var str = this.GetPropertyValue<string>(alias);
    
                    if (str != null)
                    {
                            return _umbracoHelper.TypedMedia(str);
                    }
                    return null;
                }
    
    
    
        }
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 26, 2016 @ 10:43
    Jeavon Leopold
    101

    It's an interesting one with moving the helpers in Umbraco to HtmlHelper and UrlHelper extension methods rather than IPublishedContent which I've continued in this package it means you need a helper to use the methods in this context.

    I would go with this.

    public abstract class BaseContentType : PublishedContentModel
    {
    
        private UmbracoHelper _umbracoHelper;
        internal UrlHelper UrlHelper;
    
        protected BaseContentType(IPublishedContent content)
                : base(content)
        {
            _umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            UrlHelper = new UrlHelper();
        }
    

    and then

    public class Image : BaseContentType
    {
    
        public Image(IPublishedContent content) : base(content)
        {
        }
    
        public string Url
        {
            get
            {
                return this.UrlHelper.GetCropCdnUrl(this.Content, imageCropMode: ImageCropMode.Max, width: 1200).ToString();
            }
        }
    }
    

    I will think about adding methods that aren't extension methods to this package as I think they are useful on their own for scenarios such as this...

  • Laurence Gillian 600 posts 1219 karma points
    Sep 26, 2016 @ 14:25
    Laurence Gillian
    0

    Thanks Jeavon! I've successfully implemented everywhere, just waiting for the blobs to be available on the CDN endpoint :-) Thanks! Laurie

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 26, 2016 @ 14:31
    Jeavon Leopold
    1

    Cool, it can take quite a few hours before they appear, it says 90 minutes but I've never seen it be that quick!

  • Simon Dingley 1474 posts 3431 karma points c-trib
    May 24, 2017 @ 09:45
    Simon Dingley
    0

    This helped me since I have code outside of Razor views where I want to return Crops from the CDN and came unstuck! Thanks

  • Laurence Gillian 600 posts 1219 karma points
    Sep 27, 2016 @ 08:57
    Laurence Gillian
    0

    The files are there now!

    Just having some issues with the ImageProcessor part of it >.<

    Using the latest (AzureBlobCache.1.1.2.0) as my storage is in the UK.

    Have checked all the DLL's are present, and versions are correct, triple checked config files!

    System.MissingMethodException: Method not found: 'System.String ImageProcessor.Web.Helpers.ImageHelpers.GetExtension(System.String, System.String)'.
    at ImageProcessor.Web.Plugins.AzureBlobCache.AzureBlobCache.<CreateCachedFileNameAsync>d__24.MoveNext()
        at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
           at ImageProcessor.Web.Plugins.AzureBlobCache.AzureBlobCache.CreateCachedFileNameAsync()
                at ImageProcessor.Web.Plugins.AzureBlobCache.AzureBlobCache.<IsNewOrUpdatedAsync>d__2.MoveNext()
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 27, 2016 @ 09:01
    Jeavon Leopold
    1

    Odd, could you try updating the WindowsAzure.Storage NuGet package to the latest (I have a hunch something's slightly different with new UK accounts)?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 27, 2016 @ 09:23
    Jeavon Leopold
    1

    To double check you have the following versions?

    • ImageProcessor.Web.Plugins.AzureBlobCache v1.1.2
    • ImageProcessor.Web v4.6.4
    • ImageProcessor v2.4.4
    • ImageProcessor.Web.Config v2.3.0
  • Laurence Gillian 600 posts 1219 karma points
    Sep 27, 2016 @ 13:02
    Laurence Gillian
    1

    Updating WindowsAzure.Storage from 6.2.0 to 7.2.1 resolved this issue.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 27, 2016 @ 13:10
    Jeavon Leopold
    1

    Oh wow!

  • Laurence Gillian 600 posts 1219 karma points
    Sep 27, 2016 @ 13:17
    Laurence Gillian
    1

    Yep! Thanks again :-) Looking forward to Slimsy v2

Please Sign in or register to post replies

Write your reply to:

Draft