Umbraco中经常需要使用到RelatedLink, 那么在代码中我们如何来获取RelatedLink呢,

可能在Backoffice中我们有一个RelatedLink, 上面有3个链接,如下所示:

我们在项目开发过程中,有两种方式来处理它们

方式1 :  采用 JArray  (引用命名空间  Newtonsoft.Json.Linq)

using Newtonsoft.Json.Linq;

public static class Helper
{
public static List<RelatedLink> GetRelatedLinks(IPublishedContent contentItem, string propertyAlias)
{
List<RelatedLink> relatedLinksList = null; UmbracoHelper uHelper = new UmbracoHelper(UmbracoContext.Current);
IPublishedProperty relatedLinkProperty = contentItem.GetProperty(propertyAlias); if (relatedLinkProperty != null && relatedLinkProperty.HasValue && relatedLinkProperty.Value.ToString().Length > )
{
relatedLinksList = new List<RelatedLink>();
JArray relatedLinks = (JArray)relatedLinkProperty.Value; foreach (JObject linkItem in relatedLinks)
{
string linkUrl = (linkItem.Value<bool>("isInternal")) ? uHelper.NiceUrl(linkItem.Value<int>("internal")) : linkItem.Value<string>("link");
bool newWindow = linkItem.Value<bool>("newWindow");
string linkText = linkItem.Value<string>("caption");
relatedLinksList.Add(new RelatedLink(linkText, linkUrl, newWindow));
}
} return relatedLinksList; } }

方式2:  直接使用RelatedLinks的属性

在后端,我们把空的链接删除,写了一个静态的扩展方法

public static IEnumerable<RelatedLink> FilterEmptyLinks(this RelatedLinks links)
{
var filtered = links?.Where(l => !string.IsNullOrWhiteSpace(l.Link) && l.Link != "#"); return filtered ?? Enumerable.Empty<RelatedLink>(); }

在前端cshtml页面的razor文件中,如下操作

@foreach(var link in Model.Content.LoginLinks.FilterEmptyLinks())
{
<li>
<a href="@(link.Link)" target="@(link.NewWindow ? "_blank" : null)">@link.Caption</a>
</li> }

方法3 在一个项目中,我们写了一个RelatedLinkModel, 然后又在helper.cs中写了一个GetsRelatedLinks方法来获取, 但这个好像只是适合Umbraco 7.6的 (Obsolete)Related links

using Newtonsoft.Json;
using MyProject.Models public class RelatedLinkModel
{ [JsonProperty(PropertyName = "link")]
public string Url {get; set;} [JsonProperty(PropertyName = "caption")]
public string Caption {get; set;} [JsonProperty(PropertyName = "title")]
public string Title {get; set;} [JsonProperty(PropertyName = "newWindow")]
public bool IsNewWindow {get; set;} [JsonProperty(PropertyName = "internal")]
public int? InternalId {get; set;} public string LinkUrl {get; set;} }

然后,在Helper.cs中有一个方法 GetsRelatedLinks

using Newtonsoft.Json
using MyProject.Models public static List<RelatedLinkModel> GetsRelatedLinks(this IPublishedContent content, string propertyAlias)
{ if(content == null)
return new List<RelatedLinkModel>(); if(content.HasValue(propertyAlias) && ((string)content.GetProperty(propertyAlias).DataValue).Length > )
{
var json = ((string)content.GetProperty(propertyAlias).DataValue);
List<RelatedLinkModel> relatedLinks = JsonConvert.DeserializeObject<List<RelatedLinkModel>>(json);
foreach(RelatedLinkModel current in relatedLinks)
{
if(current.InternalId.HasValue)
{
var contentU = new UmbracoHelper(ContextHelpers.EnsureUmbracoContext()).TypedContent(current.InternalId.Value);
if(contentU != null)
{
current.LinkUrl = contentU.Url;
}
else
{
current.LinkUrl = "";
} }
else if (!string.IsNullOrEmpty(current.Url))
{
current.LinkUrl = current.Url;
} }
return relatedLinks;
}
return null;
}

Umbraco中的RelatedLink的使用的更多相关文章

  1. Umbraco中的ModelBuilder

    Umbraco中的ModelBuilder有以下几种形式 Pure Live models Dll models LiveDll models AppData models LiveAppData m ...

  2. Umbraco中Document Type取名限制

    在Umbraco中,每一个Document type都会被ModelsBuilder生成一个class,但是,有些developers发现,当你把一些Document Type命名为Grid, Pro ...

  3. [Umbraco] umbraco中如何分页

    分页功能应该说是web开发中最基本的功能了,常规的做法是通过查询sql语句进行分页数据显示.但在umbraco中却不是这样子的.而且通过xpath中的postion来定位.如下代码 <?xml ...

  4. [Umbraco] macro(宏)在umbraco中的作用

    macro在umbraco中是一个核心的应用,它是模板页中用于动态加载内容的标签(模板指令),宏可以是基于XSLT文件创建,亦可以是基于ASP.NET用户控件创建 在develop下的Macros中创 ...

  5. Umbraco中使用Related Links显示内部链接和外部链接

    在Umbraco的论坛里看到的办法,演示了如何在Umbraco中使用Related Links并显示的过程. 原文地址:http://www.nibble.be/?p=48

  6. Umbraco中根据ID获取IPublishedContent

    Umbraco中根据ID来获取IPublishedContent 在Umbraco网站上的 https://our.umbraco.com/documentation/Reference/Templa ...

  7. 高级搜索插件solis search在umbraco中的使用

    好久没有写关于umbraco的博客了,这段时间在研究solis search,感觉它太强大,好东西是需要分享的,所以写一篇简单的使用博客分享给个人umbraco爱好者. 简介 在了解solis sea ...

  8. Umbraco中更换IndexSet中的NodeType后,搜索页面没有做出对应更改的效果

    在项目开发中,使用ExternalSearcher,有一个ExamineIndex.config文件中存放ExternalIndexSet 开始时是这样的 <!-- Default Indexs ...

  9. Umbraco中的Member登录时的Lock out功能

    请参看文章 https://our.umbraco.org/forum/using-umbraco-and-getting-started/76389-preventing-member-lock-o ...

随机推荐

  1. 【JavaScript学习整理】DOM对象(location history screen navigator)

    DOM: 描述网页各个组成部分之间的关系. parentNode: 父节点 childNode: 子节点 firstChild: 第一个子节点 lastChild: 最后一个子节点 nextSibli ...

  2. keepalvied虚拟ip安装设置

    keepalvied 虚拟ip 第一步:主服务器上设置虚拟ip (也可以不设置虚拟ip,keepalived启动的时候会自动设置) ifconfig ens33:1 192.168.136.131 n ...

  3. eDocEngine_3.0.4.273的手动安装

    1.安装FastReport 5: 2.编译Shared3(如对Delphi2007,打开gtSharedD11.groupproj项目文件),将产生的bpl.dcp文件分别拷贝到C:\Users\P ...

  4. codevs1217 借教室

    借教室(classroom.cpp/c/pas)[问题描述]在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室 ...

  5. Linux- 关于windows和Linux和Mac的换行符

    windows 的换行符为"\r\n" Linux的换行符为"\n" Mac的换行符为"\n\r",和Windows相反

  6. ava:Map借口及其子类HashMap三

    ava:Map借口及其子类HashMap三 HashMap常用子类(异步非安全线程,性能高: Hashtable:同步的安全线程,性能低) map(HashMap)中的key,value可以通过 Se ...

  7. 我理解的关于Vue.nextTick()的正确使用

    什么是Vue.nextTick() 官方文档解释如下: 在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 我理解的官方文档的这句话的侧重点在最后那半 ...

  8. python--17个新手常见Python运行时错误

    当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...

  9. 原生JS实现淡入淡出效果(fadeIn/fadeOut/fadeTo)

    淡入淡出效果,在日常项目中经常用到,可惜原生JS没有类似的方法,而有时小的页面并不值得引入一个jQuery库,所以就自己写了一个,已封装, 有用得着的朋友, 可以直接使用. 代码中另附有一个设置元素透 ...

  10. 关于ATML信号定义的理解-1

    1.XML中的类型标签: <xs:complexType>复合类型和<xs:simpleTyle>简单类型是数据结构类型,包含了各种类型的属性.可以被子类型继承,继承方式为&l ...