zoukankan      html  css  js  c++  java
  • 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 > 2)
                {
                    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 > 2)
    {
       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;
    }
  • 相关阅读:
    POJ
    UPC-5843: 摘樱桃(最优状态递推)
    BZOJ-1088 [SCOI2005]扫雷Mine(递推+思维)
    HDU-3065 病毒侵袭持续中(AC自动机)
    BZOJ-4236 JOIOJI (map查找+思维)
    HDU-2896 病毒侵袭(AC自动机)
    Hrbust-2060 截取方案数(KMP)
    UVALive 4490 压缩DP
    UVALive 5881
    UVALive 4168
  • 原文地址:https://www.cnblogs.com/wphl-27/p/9795792.html
Copyright © 2011-2022 走看看