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;
}

最新文章

  1. Hystrix框架2--超时
  2. 【转】Java内存管理:深入Java内存区域
  3. HorizontalScrollView
  4. HTML控件ID和NAME属性及在CS页面获得.ASPX页面中HTML控件的值
  5. 表单_post提交方式和get的区别,元素集
  6. sql,mybatis,javascript分页功能的实现
  7. Sql Server尝试读取或写入受保护的内存。这通常指示其他内存已损坏
  8. Recycleview实现复杂布局
  9. R语言使用 multicore 包进行并行计算
  10. Java文件输入保存,统计某个字符串,统计所有字符串
  11. encode和decode区别
  12. js 正则进阶regexp
  13. Delphi 之弹出气泡消息提示
  14. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: ..... this is incompatible with sql_mode=only_full_group_by
  15. 【mysql】update的in的嵌套查询更新,如果字段中包含字符串A,统一替换为字符串B
  16. CSS 盒子模型 二
  17. (转)python中math模块常用的方法整理
  18. c语言文法定义
  19. Oracle数据稠化
  20. C# Params的应用

热门文章

  1. 基于msm8909高通平台Android驱动开发之hello程序
  2. 算法(Algorithms)第4版 练习 1.3.1
  3. maven命令创建项目
  4. Codeforces 876C Classroom Watch:枚举
  5. spring学习(4)
  6. thinkphp3.2新部署是错
  7. 纯css实现3D字体
  8. linux-常用指令2
  9. leetcode 205. Isomorphic Strings(哈希表)
  10. 1030 Travel Plan (30)(30 分)