Exemplo n.º 1
0
        public virtual IPublishedContent GetByRoute(UmbracoContext umbracoContext, bool preview, string route, bool?hideTopLevelNode = null)
        {
            if (route == null)
            {
                throw new ArgumentNullException("route");
            }

            // try to get from cache if not previewing
            var contentId = preview ? 0 : _routesCache.GetNodeId(route);

            // if found id in cache then get corresponding content
            // and clear cache if not found - for whatever reason
            IPublishedContent content = null;

            if (contentId > 0)
            {
                content = GetById(umbracoContext, preview, contentId);
                if (content == null)
                {
                    _routesCache.ClearNode(contentId);
                }
            }

            // still have nothing? actually determine the id
            hideTopLevelNode = hideTopLevelNode ?? GlobalSettings.HideTopLevelNodeFromPath; // default = settings
            content          = content ?? DetermineIdByRoute(umbracoContext, preview, route, hideTopLevelNode.Value);

            // cache if we have a content and not previewing
            if (content != null && !preview)
            {
                var domainRootNodeId = route.StartsWith("/") ? -1 : int.Parse(route.Substring(0, route.IndexOf('/')));
                var iscanon          = !UnitTesting && !DomainHelper.ExistsDomainInPath(DomainHelper.GetAllDomains(false), content.Path, domainRootNodeId);
                // and only if this is the canonical url (the one GetUrl would return)
                if (iscanon)
                {
                    _routesCache.Store(contentId, route);
                }
            }

            return(content);
        }