Umbraco 13升级后UmbracoHelper获取Path返回负数串问题求助
解决Umbraco 13中获取节点友好路径的问题
在Umbraco 13里,IPublishedContent的Path属性本质是内部存储的节点ID层级链(格式如-1,1440,5787),并非UI中显示的友好路径。要获取正确的路径字符串,有两种常用方案:
方案1:获取可直接访问的URL路径(推荐)
利用Umbraco的UrlProvider生成符合站点路由规则的实际访问路径,支持URL别名、多语言等场景。
修改你的代码中Select部分的Path赋值:
.Select(node => new SearchResult { Id = node.Id, Name = node.Name, Content = node.Value<string>("tekst"), // 获取相对路径 Path = _umbracoHelper.Url(node.Id), // 若需要绝对路径,使用: // Path = _umbracoHelper.Url(node.Id, UrlMode.Absolute) }).ToList();
方案2:获取与UI一致的层级名称路径
如果需要和Umbraco后台UI显示的路径完全一致(基于节点名称拼接,不考虑URL别名),可以遍历节点的祖先链拼接名称:
先添加一个辅助方法:
private string GetHierarchicalPath(IPublishedContent node) { var segments = new List<string>(); var current = node; // 遍历直到根节点(跳过系统根节点-1) while (current != null && current.Id != -1) { segments.Insert(0, current.Name); current = current.Parent; } return "/" + string.Join("/", segments); }
然后在Select中调用:
.Select(node => new SearchResult { Id = node.Id, Name = node.Name, Content = node.Value<string>("tekst"), Path = GetHierarchicalPath(node) }).ToList();
内容的提问来源于stack exchange,提问作者jtveter
相关产品推荐
相关产品推荐

