/// <summary> /// Gets the other urls of a published content. /// </summary> /// <param name="umbracoContext">The Umbraco context.</param> /// <param name="id">The published content id.</param> /// <param name="current">The current absolute url.</param> /// <returns>The other urls for the published content.</returns> /// <remarks> /// <para>Other urls are those that <c>GetUrl</c> would not return in the current context, but would be valid /// urls for the node in other contexts (different domain for current request, umbracoUrlAlias...).</para> /// </remarks> public virtual IEnumerable <string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current) { // will not use cache if previewing var route = umbracoContext.ContentCache.GetRouteById(id); if (string.IsNullOrWhiteSpace(route)) { LogHelper.Debug <DefaultUrlProvider>( "Couldn't find any page with nodeId={0}. This is most likely caused by the page not being published.", () => id); return(null); } if (route.StartsWith("err/")) { return(null); } var domainHelper = new DomainHelper(umbracoContext.Application.Services.DomainService); // extract domainUri and path // route is /<path> or <domainRootId>/<path> var pos = route.IndexOf('/'); var path = pos == 0 ? route : route.Substring(pos); var domainUris = pos == 0 ? null : domainHelper.DomainsForNode(int.Parse(route.Substring(0, pos)), current); // assemble the alternate urls from domainUris (maybe empty) and path return(AssembleUrls(domainUris, path).Select(uri => uri.ToString())); }
/// <summary> /// Gets the other urls of a published content. /// </summary> /// <param name="umbracoContext">The Umbraco context.</param> /// <param name="id">The published content id.</param> /// <param name="current">The current absolute url.</param> /// <returns>The other urls for the published content.</returns> /// <remarks> /// <para>Other urls are those that <c>GetUrl</c> would not return in the current context, but would be valid /// urls for the node in other contexts (different domain for current request, umbracoUrlAlias...).</para> /// </remarks> public IEnumerable <string> GetOtherUrls(UmbracoContext umbracoContext, int id, Uri current) { if (!FindByUrlAliasEnabled) { return(Enumerable.Empty <string>()); // we have nothing to say } var node = umbracoContext.ContentCache.GetById(id); string umbracoUrlName = null; if (node.HasProperty(Constants.Conventions.Content.UrlAlias)) { umbracoUrlName = node.GetPropertyValue <string>(Constants.Conventions.Content.UrlAlias); } if (string.IsNullOrWhiteSpace(umbracoUrlName)) { return(Enumerable.Empty <string>()); } var domainHelper = new DomainHelper(umbracoContext.Application.Services.DomainService); var n = node; var domainUris = domainHelper.DomainsForNode(n.Id, current, false); while (domainUris == null && n != null) // n is null at root { // move to parent node n = n.Parent; domainUris = n == null ? null : domainHelper.DomainsForNode(n.Id, current, false); } var path = "/" + umbracoUrlName; if (domainUris == null) { var uri = new Uri(path, UriKind.Relative); return(new[] { UriUtility.UriFromUmbraco(uri).ToString() }); } return(domainUris .Select(domainUri => new Uri(CombinePaths(domainUri.Uri.GetLeftPart(UriPartial.Path), path))) .Select(uri => UriUtility.UriFromUmbraco(uri).ToString())); }