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 .
e.g. we have 3 sites setup in the following order
- portal.mysite.com.au
- mysite.com.au
- 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
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.
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;
}
}