我们有一个多站点解决方案(站点 1 和站点 2),我需要能够确定我们获取 URL 的项目(在自定义的 LinkProvider 中)是否属于当前上下文站点( Sitecore.Context.Site
),或者是其他站点的一部分。有没有好的方法来做到这一点?
基本上,我们只需要能够找出该项目与哪个站点相关联。我们可以将该值与当前上下文站点进行比较。
最佳答案
/// <summary>
/// Get the site info from the <see cref="SiteContextFactory"/> based on the item's path.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>The <see cref="SiteInfo"/>.</returns>
public static SiteInfo GetSiteInfo(this Item item)
{
return SiteContextFactory.Sites
.Where(s => !string.IsNullOrWhiteSpace(s.RootPath) && item.Paths.Path.StartsWith(s.RootPath, StringComparison.OrdinalIgnoreCase))
.OrderByDescending(s => s.RootPath.Length)
.FirstOrDefault();
}
关于sitecore - 如何找出项目与哪个 Sitecore 站点相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200864/