private string GetRouteByIdInternal(bool preview, int contentId, bool?hideTopLevelNode, string culture)
        {
            var node = GetById(preview, contentId);

            if (node == null)
            {
                return(null);
            }

            hideTopLevelNode = hideTopLevelNode ?? HideTopLevelNodeFromPath; // default = settings

            // walk up from that node until we hit a node with a domain,
            // or we reach the content root, collecting urls in the way
            var pathParts  = new List <string>();
            var n          = node;
            var urlSegment = n.UrlSegment(culture);
            var hasDomains = _domainCache.HasAssigned(n.Id);

            while (hasDomains == false && n != null) // n is null at root
            {
                // no segment indicates this is not published when this is a variant
                if (urlSegment.IsNullOrWhiteSpace())
                {
                    return(null);
                }

                pathParts.Add(urlSegment);

                // move to parent node
                n = n.Parent;
                if (n != null)
                {
                    urlSegment = n.UrlSegment(culture);
                }

                hasDomains = n != null && _domainCache.HasAssigned(n.Id);
            }

            // at this point this will be the urlSegment of the root, no segment indicates this is not published when this is a variant
            if (urlSegment.IsNullOrWhiteSpace())
            {
                return(null);
            }

            // no domain, respect HideTopLevelNodeFromPath for legacy purposes
            if (hasDomains == false && hideTopLevelNode.Value)
            {
                ApplyHideTopLevelNodeFromPath(node, pathParts, preview);
            }

            // assemble the route
            pathParts.Reverse();
            var path = "/" + string.Join("/", pathParts); // will be "/" or "/foo" or "/foo/bar" etc
            //prefix the root node id containing the domain if it exists (this is a standard way of creating route paths)
            //and is done so that we know the ID of the domain node for the path
            var route = (n?.Id.ToString(CultureInfo.InvariantCulture) ?? "") + path;

            return(route);
        }
Exemple #2
0
        string DetermineRouteById(bool preview, int contentId)
        {
            var node = GetById(preview, contentId);

            if (node == null)
            {
                return(null);
            }

            // walk up from that node until we hit a node with a domain,
            // or we reach the content root, collecting urls in the way
            var pathParts  = new List <string>();
            var n          = node;
            var hasDomains = _domainCache.HasAssigned(n.Id);

            while (hasDomains == false && n != null) // n is null at root
            {
                // get the url
                var urlName = n.UrlSegment();
                pathParts.Add(urlName);

                // move to parent node
                n          = n.Parent;
                hasDomains = n != null && _domainCache.HasAssigned(n.Id);
            }

            // no domain, respect HideTopLevelNodeFromPath for legacy purposes
            if (hasDomains == false && _globalSettings.HideTopLevelNodeFromPath)
            {
                if (node.Parent == null)
                {
                    var rootNode = GetByRoute(preview, "/", true);
                    if (rootNode == null)
                    {
                        throw new Exception("Failed to get node at /.");
                    }
                    if (rootNode.Id == node.Id) // remove only if we're the default node
                    {
                        pathParts.RemoveAt(pathParts.Count - 1);
                    }
                }
                else
                {
                    pathParts.RemoveAt(pathParts.Count - 1);
                }
            }

            // assemble the route
            pathParts.Reverse();
            var path  = "/" + string.Join("/", pathParts); // will be "/" or "/foo" or "/foo/bar" etc
            var route = (n?.Id.ToString(CultureInfo.InvariantCulture) ?? "") + path;

            return(route);
        }