Umbraco中的RelatedLink的使用
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的使用的更多相关文章
- Umbraco中的ModelBuilder
Umbraco中的ModelBuilder有以下几种形式 Pure Live models Dll models LiveDll models AppData models LiveAppData m ...
- Umbraco中Document Type取名限制
在Umbraco中,每一个Document type都会被ModelsBuilder生成一个class,但是,有些developers发现,当你把一些Document Type命名为Grid, Pro ...
- [Umbraco] umbraco中如何分页
分页功能应该说是web开发中最基本的功能了,常规的做法是通过查询sql语句进行分页数据显示.但在umbraco中却不是这样子的.而且通过xpath中的postion来定位.如下代码 <?xml ...
- [Umbraco] macro(宏)在umbraco中的作用
macro在umbraco中是一个核心的应用,它是模板页中用于动态加载内容的标签(模板指令),宏可以是基于XSLT文件创建,亦可以是基于ASP.NET用户控件创建 在develop下的Macros中创 ...
- Umbraco中使用Related Links显示内部链接和外部链接
在Umbraco的论坛里看到的办法,演示了如何在Umbraco中使用Related Links并显示的过程. 原文地址:http://www.nibble.be/?p=48
- Umbraco中根据ID获取IPublishedContent
Umbraco中根据ID来获取IPublishedContent 在Umbraco网站上的 https://our.umbraco.com/documentation/Reference/Templa ...
- 高级搜索插件solis search在umbraco中的使用
好久没有写关于umbraco的博客了,这段时间在研究solis search,感觉它太强大,好东西是需要分享的,所以写一篇简单的使用博客分享给个人umbraco爱好者. 简介 在了解solis sea ...
- Umbraco中更换IndexSet中的NodeType后,搜索页面没有做出对应更改的效果
在项目开发中,使用ExternalSearcher,有一个ExamineIndex.config文件中存放ExternalIndexSet 开始时是这样的 <!-- Default Indexs ...
- Umbraco中的Member登录时的Lock out功能
请参看文章 https://our.umbraco.org/forum/using-umbraco-and-getting-started/76389-preventing-member-lock-o ...
随机推荐
- java常用注解(更新中)
注解根据来源可分为: 系统注解(自带的,取决于JDK版本).自定义注解及第三方注解 系统注解根据用途又可分为: java内置注解和元注解 根据运行机制(保留到什么时候)可分为: 源码注解.编译注解和运 ...
- LINQ 学习路程 -- 查询操作 ThenBy & ThenByDescending
IList<Student> studentList = new List<Student>() { , StudentName = } , , StudentName = } ...
- 算法(Algorithms)第4版 练习 1.3.3
(a) 4 3 2 1 0 9 8 7 6 5 (b) 4 6 8 7 5 3 2 9 0 1 (c) 2 5 6 7 4 8 9 3 1 0 (d) 4 3 2 1 0 5 6 7 8 9 (e) ...
- Hive- Hive 的基本操作
创建数据库 create database db_hive; use db_hive; create database if not exists db_hive_02; create databas ...
- python3 字典属性
1.字典创建 >>> D={} >>> D {} >>> D2={:,(,):::'d'}} #冒号构造 1.使用 { }和 : 直接创建 &g ...
- 未测试 Delphi读写UTF-8、Unicode格式文本文件
// UTF-8文件写入函数 procedure SaveUTFFile(const FileName: string; S: string; WriteHeader: Boolean = True) ...
- POJ 1577 Falling Leaves(二叉搜索树)
思路:当时学长讲了之后,似乎有点思路----------就是倒着建一个 二叉搜索树 代码1:超时 详见超时原因 #include<iostream> #include<cstrin ...
- Python基础-操作mysql
mysql 属于第三方模块,需要先安装 pip install pymysql,sql执行后,数据获取函数有三种cur.fetchone()#获取第一条数据,依次类推下去,第二次执行时候,就会取除去第 ...
- 2016北京集训 小Q与进位制
题目大意 一个数每一位进制不同,已知每一位的进制,求该数的十进制表达. 显然有 $$Ans=\sum\limits_{i=0}^{n-1}a_i \prod\limits_{j=0}^{i-1}bas ...
- 1143. Lowest Common Ancestor (30)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...