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