Copied to clipboard

Flag this post as spam?

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


  • Joseph 59 posts 140 karma points
    Aug 19, 2013 @ 00:11
    Joseph
    0

    Getting a node's NiceURL when UmbracoContext is null

    I'm currently working on some code which auto-tweets an article when it is published. This works fine when it is manually published but runs into problems when it is set up to be published at a particular date/time: I narrowed the problem down to the fact that UmbracoContext.Current is null (potentially because HttpContext.Current is null) when the publish is instigated by a separate thread. This means that Node.NiceUrl returns "#".

    Do I have any alternative means to retrieving the NiceUrl given a node ID? The site I'm working on is on Umbraco 4.7.1.1.

  • Joseph 59 posts 140 karma points
    Aug 21, 2013 @ 22:51
    Joseph
    0

    In the end, I built a non-HttpContext dependent implementation of NiceUrl based on the Umbraco source. The source is below. Note that for simplicity I stripped away parts of the original implementation that did not apply to me. YMMV.

        private static readonly Dictionary<int, string> NiceUrlCache = new Dictionary<int, string>();
    
        private static readonly object Locker = new object();
    
        public static string NiceUrl(int nodeID)
        {
            try
            {
                int startNodeDepth = 1;
                if (GlobalSettings.HideTopLevelNodeFromPath)
                    startNodeDepth = 2;
                return NiceUrlDo(nodeID, startNodeDepth);
            }
            catch
            {
                return "#";
            }
        }
    
        private static string NiceUrlDo(int nodeID, int startNodeDepth)
        {
            if (!NiceUrlCache.ContainsKey(nodeID))
            {
                lock (Locker)
                {
                    if (!NiceUrlCache.ContainsKey(nodeID))
                    {
                        var niceUrl = NiceUrlJuno(nodeID, startNodeDepth);
                        if (!string.IsNullOrEmpty(niceUrl))
                            NiceUrlCache.Add(nodeID, niceUrl);
                    }
                }
            }
            return NiceUrlCache[nodeID];
        }
    
        private static string NiceUrlJuno(int nodeId, int startNodeDepth)
        {
            var str = string.Empty;
            var elementById = new Node(nodeId);
    
            if (elementById.Parent != null || UmbracoSettings.UseDomainPrefixes)
            {
                if ((UmbracoSettings.UseDomainPrefixes) && Domain.GetDomainsById(nodeId).Length > 0)
                    return GetDomain();
                if (str == string.Empty && (elementById.Level > startNodeDepth || UmbracoSettings.UseDomainPrefixes) && elementById.Parent.Name != "root")
                    str = NiceUrlJuno(elementById.Parent.Id, startNodeDepth);
            }
            if (elementById.Level >= startNodeDepth)
                return str + "/" + elementById.UrlName;
            if (elementById.Parent == null) //if (elementById.PreviousSibling != null)
                return "/" + elementById.UrlName;
    
            return "/";
        }
    
        public static string GetDomain()
        {
            var hostName = ConfigurationManager.AppSettings["HostName"];
            return "http://" + hostName;
        } 
    
Please Sign in or register to post replies

Write your reply to:

Draft