微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Umbraco - 从文本而不是 nodeId 获取内容

如何解决Umbraco - 从文本而不是 nodeId 获取内容

我的项目中目前有以下代码

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
IPublishedContent currentContent = umbracoHelper.TypedContent(UmbracoContext.Current.PageId);
var contentService = UmbracoContext.Current.Application.Services.ContentService;

int configPageId = 0;

if(currentContent.AncestorsOrSelf().Select(x => x.Id).ToArray().Contains(1309))
{
    configPageId = 1872;
} 
else if(currentContent.AncestorsOrSelf().Select(x => x.Id).ToArray().Contains(1308))
{
    configPageId = 1660;
}
if(configPageId != 0)
{
    IContent content = contentService.GetById(configPageId);
    linkCreditSimulator = content.GetValue("linkCreditSimulator").ToString();
    linkAskNow = content.GetValue("linkAskNow").ToString();
}

我认为无论是谁做的,都是因为不同的环境有不同的 id。

我希望将其正常化。

如果我运行以下查询

    SELECT TOP (1000)  d.text
  FROM [cmsContent] c
  LEFT join cmsContentXml cxml on c.nodeId = cxml.nodeId
  LEFT join cmsMedia m on m.nodeId = c.nodeId
  LEFT join cmsDocument d on d.nodeId = c.nodeId
  LEFT join cmsMedia cm on cm.nodeId = c.nodeId
  LEFT join cmsMember mb on mb.nodeId = c.nodeId
  LEFT join cmsPreviewXml p on p.nodeId = c.nodeId
  LEFT join cmsTagrelationship tr on tr.nodeId = c.nodeId
  where c.nodeId = 1872

我得到值“模拟器”,这在不同环境中是相同的。

我试图在 contentService 中找到类似于

的任何方法
IContent content = contentService.GetByName("Simulator");

但是我做不到。你知道是否有办法实现这一目标? 您还推荐什么其他方法

最好的问候

解决方法

假设“configPage”是特定的内容类型,您可以使用其别名查询该类型:https://our.umbraco.com/documentation/Reference/Querying/UmbracoHelper/#contentatxpathstring-xpath - 这样您只需对别名进行硬编码,而不是像您说的那样对一堆 ID 进行硬编码,可以并且将会区分环境。

ContentService 的使用也应保持在最低限度,因为它使用数据库访问(如您示例中的大型 SQL 查询)而不是缓存。 ContentAtXPath 使用缓存。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。