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 ...
随机推荐
- Drools Fusion (CEP) Example 和 关键概念
Drools Fusion (Complex Event Processing) 是Drools对于复杂事件处理的模块, 与它功能相似的是Esper, 两者都可以提供基于时间跨度和滑动窗口的事件处理, ...
- scala基本学习
def addOne(f: Int => Int, arg: Int) = f(arg) + 1,意思是 addOne要两个参数一个是:传一个整数的参数且返回一个整形的方法的参数,第二个参数就是 ...
- PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP
/** * 检测访问的ip是否为规定的允许的ip * Enter description here ... */ function check_ip(){ $ALLOWED_IP=array('192 ...
- python习题-用交集方式产生随机密码
# 1.写一个产生密码的程序,# 输入次数,输入多少次就产生多少条数据,# 要求密码必须包含大写字母.小写字母和数字,长度8位,不能重复 import string ,random num=input ...
- Java_注解_00_资源贴
1.Java注解教程:自定义注解示例,利用反射进行解析 2. (1)深入理解Java:注解(Annotation)基本概念 (2)深入理解Java:注解(Annotation)自定义注解入门 (3)深 ...
- stl_stack.h
stl_stack.h // Filename: stl_stack.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http:/ ...
- 【构建二叉树】01根据前序和中序序列构造二叉树【Construct Binary Tree from Preorder and Inorder Traversal】
我们都知道,已知前序和中序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 前序遍历序列, O================= 中序遍 ...
- LeetCode 510. Inorder Successor in BST II
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...
- vue2.0中的$router 和 $route的区别
1.router是VueRouter的一个对象,通过Vue.use(VueRouter)和VueRouter构造函数得到一个router的实例对象,这个对象中是一个全局的对象,他包含了所有的路由包含了 ...
- JS性能之setTimeout与clearTimeout
测试环境: chrome浏览器 结论: 1 一个页面用setTimeout越多,该页面消耗的内存就会越多,几乎成正比. 2 在'startCount(1000000);-->100万'情况下,不 ...