Skip to main content
edited title
Link
Mark Cassidy
  • 25.4k
  • 7
  • 59
  • 117

Sitecore 9.3 LinkManager generating url for wrong site intermittently

added 8 characters in body
Source Link
Tiger
  • 33
  • 4

We have Sitecore 9.3 multisite setup on Azure PAAS. We have been noticing a weird issue where for "internal" urls, LinkManager.GetItemUrl is intermittently generating urls for different site and not the context site intermittently.

  1. portal.mysite.com.au
  2. mysite.com.au
  3. myotherstemyothersite.com.au

mysite-portalportal.mysite.com.au has navigation links to transactional pages like, update personal details and change password, e.g. portal.mysite.com.au/update-password

  public static string LinkFieldUrl(this Item item, ID fieldID, bool includeServerName = false, bool siteResolving = true, SiteContext site = null, bool disableSecurity = false)
        {
            if (item == null)
                throw new ArgumentNullException(nameof(item));

            LinkField linkField = null;
            Item targetItem = null;
            if (disableSecurity)
            {
                using (new SecurityDisabler())
                {
                    linkField = item.LinkField(fieldID);
                    targetItem = linkField?.TargetItem;
                }
            }
            else
            {
                linkField = item.LinkField(fieldID);
                targetItem = linkField?.TargetItem;
            }

            if (linkField == null)
                return string.Empty;

            switch (linkField.LinkType.ToLower())
            {
                case "internal":
                    // Use LinkMananger for internal links, if link is not empty
                    var options = LinkManager.GetDefaultUrlBuilderOptions();
                    options.AlwaysIncludeServerUrl = includeServerName;
                    options.SiteResolving = siteResolving;

                    if (site != null)
                        options.Site = site;
                    if (targetItem == null) return string.Empty;
                    var url = LinkManager.GetItemUrl(targetItem, options);
                    var queryString = item.LinkFieldQuerystring(fieldID);
                    if (!string.IsNullOrEmpty(queryString))
                    {
                        url = $"{url}?{queryString}";
                    }

                    return url;
                case "media":
                    // Use MediaManager for media links, if link is not empty
                    return targetItem != null ? MediaManager.GetMediaUrl(targetItem) : string.Empty;
                case "anchor":
                    // Prefix anchor link with # if link if not empty
                    return !string.IsNullOrEmpty(linkField.Anchor) ? "#" + linkField.Anchor : string.Empty;
                case "external":
                case "mailto":
                case "javascript":
                    return linkField.Url;
                default:
                    return string.Empty;
            }
        }

We have Sitecore 9.3 multisite setup on Azure PAAS. We have been noticing a weird issue where LinkManager.GetItemUrl is generating urls for different site and not the context site intermittently.

  1. portal.mysite.com.au
  2. mysite.com.au
  3. myotherste.com.au

mysite-portal has navigation links to transactional pages like, update personal details and change password, e.g. portal.mysite.com.au/update-password

We have Sitecore 9.3 multisite setup on Azure PAAS. We have been noticing a weird issue where for "internal" urls, LinkManager.GetItemUrl is intermittently generating urls for different site and not the context site .

  1. portal.mysite.com.au
  2. mysite.com.au
  3. myothersite.com.au

portal.mysite.com.au has navigation links to transactional pages like, update personal details and change password, e.g. portal.mysite.com.au/update-password

  public static string LinkFieldUrl(this Item item, ID fieldID, bool includeServerName = false, bool siteResolving = true, SiteContext site = null, bool disableSecurity = false)
        {
            if (item == null)
                throw new ArgumentNullException(nameof(item));

            LinkField linkField = null;
            Item targetItem = null;
            if (disableSecurity)
            {
                using (new SecurityDisabler())
                {
                    linkField = item.LinkField(fieldID);
                    targetItem = linkField?.TargetItem;
                }
            }
            else
            {
                linkField = item.LinkField(fieldID);
                targetItem = linkField?.TargetItem;
            }

            if (linkField == null)
                return string.Empty;

            switch (linkField.LinkType.ToLower())
            {
                case "internal":
                    // Use LinkMananger for internal links, if link is not empty
                    var options = LinkManager.GetDefaultUrlBuilderOptions();
                    options.AlwaysIncludeServerUrl = includeServerName;
                    options.SiteResolving = siteResolving;

                    if (site != null)
                        options.Site = site;
                    if (targetItem == null) return string.Empty;
                    var url = LinkManager.GetItemUrl(targetItem, options);
                    var queryString = item.LinkFieldQuerystring(fieldID);
                    if (!string.IsNullOrEmpty(queryString))
                    {
                        url = $"{url}?{queryString}";
                    }

                    return url;
                case "media":
                    // Use MediaManager for media links, if link is not empty
                    return targetItem != null ? MediaManager.GetMediaUrl(targetItem) : string.Empty;
                case "anchor":
                    // Prefix anchor link with # if link if not empty
                    return !string.IsNullOrEmpty(linkField.Anchor) ? "#" + linkField.Anchor : string.Empty;
                case "external":
                case "mailto":
                case "javascript":
                    return linkField.Url;
                default:
                    return string.Empty;
            }
        }
Source Link
Tiger
  • 33
  • 4

Sitecore 9.3 LinkManager generating url for wrong site intermittently

We have Sitecore 9.3 multisite setup on Azure PAAS. We have been noticing a weird issue where LinkManager.GetItemUrl is generating urls for different site and not the context site intermittently.

e.g. we have 3 sites setup in the following order

  1. portal.mysite.com.au
  2. mysite.com.au
  3. myotherste.com.au

mysite-portal has navigation links to transactional pages like, update personal details and change password, e.g. portal.mysite.com.au/update-password

We have seen from our monitoring tools that the navigation links in portal.mysite.com.au sometimes have mysite.com.au in the hostname.

e.g. Sometimes portal.mysite.com.au/update-password link url is mysite.com.au/update-password which is causing 404 Page Not Found.

Has anyone experienced similar issue and/or suggest what we could try to fix this issue.