如何在C# Kentico 12 MVC中获取别名路径/Node Alias及解决页面查询问题
在Kentico 12 MVC中获取别名路径与正确查询子页面
你遇到的问题其实是对Kentico的两个核心字段理解混淆了——NodeAlias和NodeAliasPath,这俩的区别直接导致了你查不到子页面的问题,我来给你理清楚:
为什么原来的查询无效?
NodeAlias字段只存储当前单个页面的独立别名,比如你的子页面"Another-Page",它的NodeAlias就是"Another-Page",而不是"Test-Page/Another-Page"。那个带父层级的完整别名路径,实际上存在NodeAliasPath字段里,而且它的格式是开头带斜杠的,比如/Test-Page/Another-Page。
正确查询子页面的方式
要用NodeAliasPath来做精确匹配查询,代码示例如下:
// 通用文档查询方式 var targetPage = DocumentHelper.GetDocuments() .WhereEquals("NodeAliasPath", "/Test-Page/Another-Page") // 注意开头的斜杠 .TopN(1) .FirstOrDefault(); // 如果是自定义页面类型,用强类型查询更友好 var targetPage = DocumentHelper.GetDocuments<YourCustomPageType>() .WhereEquals(nameof(YourCustomPageType.NodeAliasPath), "/Test-Page/Another-Page") .TopN(1) .FirstOrDefault();
获取页面的完整别名路径
当你成功获取到Document对象后,直接访问它的NodeAliasPath属性就能拿到完整的层级别名路径:
if (targetPage != null) { // 完整路径(带开头斜杠) string fullAliasPath = targetPage.NodeAliasPath; // 如果你需要去掉开头的斜杠,得到相对路径 string relativeAliasPath = fullAliasPath.TrimStart('/'); // 在这里做你的路径映射逻辑即可 }
小技巧:后台确认路径
不确定路径是否正确的话,你可以在Kentico后台打开目标页面的属性,找到「URL」部分,里面的「别名路径」就是你需要的NodeAliasPath值,直接复制过来用就行。
内容的提问来源于stack exchange,提问作者Ben Murphy
相关产品推荐
相关产品推荐

