0

我正在为 Umbraco 使用 Glass Mapper。在尝试建模时,我有一个类,例如:

[UmbracoType(AutoMap = true)]
    public class ImageWithLink : BaseUmbracoItem
    {
        public virtual Image Image { get; set; }
        public virtual ?? Link { get; set; }
        public virtual string Copy { get; set; }
    }

似乎没有 Sitecore 实现中的“链接”数据类型。我看到了这篇文章(http://bluetubeinc.com/blog/2014/6/glass-mapper-and-umbraco-7),他们使用“RelatedLink”数据类型,但这不存在(我检查了玻璃存储库)。

我必须自己建模吗?

编辑:这是相关链接属性类型。

4

2 回答 2

0

假设 Umbraco 有一段时间没有改变,并且你的意思是一个链接,就像一个 html 锚......

这一点我可能错了,但是从记忆中,Umbraco 在从 api 返回时没有/没有链接的概念。它作为节点 ID(或列表)返回,您必须使用扩展方法从其返回 url。

像你一样,据我在 Glass 代码库中看到的,它没有像我们在 Sitecore 中那样明确实现“链接”类型。

我的猜测是,您必须使用自定义类和委托功能自行滚动,或者使用整数映射并调用 umbraco api 方法。

我还猜想该示例中的 RelatedLink 正在映射到另一个类,该类将使用 UmbracoPropertyTypeMapper 的方式类似于我们在 Sitecore 中在类型之间所做的操作,而不是锚点中的“链接”。

我相信我们将在 V4 过程中再次查看 umbraco,因此请与 Mike 讨论将其添加为一项功能。

于 2015-02-13T16:44:29.117 回答
0

我找到了一个(相当可怕的)解决方案。Umbraco 返回 Json,所以我不得不反序列化它。你可以把它变成一个映射器。

[UmbracoType(AutoMap = true)]
    public class BaseUmbracoItem : IUmbracoItem
    {
        public virtual string Links { get; set; }

        public List<UmbracoLink> TypedLink
        {
            get
            {
                return JsonConvert.DeserializeObject<List<UmbracoLink>>(Links);
            }
        }
    }
public class UmbracoLink
    {
        public string link { get; set; }
        public string type { get; set; }
        public string title { get; set; }
        public bool newWindow { get; set; }
        public bool isInternal { get; set; }
    }

这是一个映射器版本:

public class UmbracoLinkMapper : AbstractDataMapper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UmbracoLinkMapper"/> class.
        /// </summary>
        public UmbracoLinkMapper()
        {
            ReadOnly = true;
        }

        /// <summary>
        /// Maps data from the .Net property value to the CMS value
        /// </summary>
        /// <param name="mappingContext">The mapping context.</param>
        /// <returns>The value to write</returns>
        /// <exception cref="System.NotSupportedException"></exception>
        public override void MapToCms(AbstractDataMappingContext mappingContext)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Maps data from the CMS value to the .Net property value
        /// </summary>
        /// <param name="mappingContext">The mapping context.</param>
        /// <returns>System.Object.</returns>
        public override object MapToProperty(AbstractDataMappingContext mappingContext)
        {
            var scContext = mappingContext as UmbracoDataMappingContext;
            var scConfig = Configuration as UmbracoLinkConfiguration;

            var properties = scContext.Content.Properties.Where(x => x.Alias == Configuration.PropertyInfo.Name.ToLower()).ToList();
            if (properties.Any())
            {
                var property = properties.First().Value as string;
                return JsonConvert.DeserializeObject<List<UmbracoLink>>(property).First();
            }
            return null;
        }

        /// <summary>
        /// Indicates that the data mapper will mapper to and from the property
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <param name="context">The context.</param>
        /// <returns><c>true</c> if this instance can handle the specified configuration; otherwise, <c>false</c>.</returns>
        public override bool CanHandle(AbstractPropertyConfiguration configuration, Context context)
        {
            return configuration is UmbracoLinkConfiguration;
        }
    }

 public class UmbracoLinkConfiguration : AbstractPropertyConfiguration
    {
        public bool IsLazy { get; set; }
        public bool InferType { get; set; }
    }

public class UmbracoLinkAttribute : AbstractPropertyAttribute
    {
        public bool IsLazy { get; set; }
        public bool InferType { get; set; }

        public override AbstractPropertyConfiguration Configure(PropertyInfo propertyInfo)
        {
            var config = new UmbracoLinkConfiguration { IsLazy = IsLazy, InferType = InferType };
            Configure(propertyInfo, config);
            return config;
        }
    }
于 2015-02-13T17:28:29.480 回答