我正在使用 GlassMapper V3 和 Sitecore 7.2。使用 MVC。
Sitecore项目数据通过GlappMapper管道自动映射,模型不是很复杂。 该模型已通过分部类进行了扩展,还可以自动映射子项:
[SitecoreQuery("./*[@@templatename='Slider Element']", IsRelative = true)]
public virtual IEnumerable<Slider_Element> SliderElements { get;set; }
到目前为止效果很好。但是:
如果一个或多个子项目没有当前上下文语言的版本,我无论如何都希望收到。这对于 PageEditMode 自动起作用,但不适用于正常模式。但是任何类型的语言后备都是没有用的,因为这种功能应该只适用于特定的属性。我需要的是这样的:
[SitecoreQuery("./*[@@templatename='Slider Element']", IsRelative = true, GetItemsHavingNoVersions = true)]
public virtual IEnumerable<Slider_Element> SliderElements { get;set; }
据我所知,GlassMapper 在很多方面都是可定制的,但到目前为止还没有找到任何可以添加此类功能的地方。
我还花了几个小时在网络上搜索并发现了 GlassMapper 源代码。
SitecoreService 类中有一个方法似乎可以执行部分逻辑,但我不能 100% 确定:
public IEnumerable<T> Query<T>(string query, bool isLazy = false, bool inferType = false) where T : class
{
return CreateTypes( typeof(T), () => { return Database.SelectItems(query); }, isLazy, inferType) as IEnumerable<T>;
}
预先非常感谢您的帮助!
最诚挚的问候, 托马斯
最佳答案
您是否尝试过使用VersionCountDisabler
将调用代码包装在 using 语句中
using(new VersionCountDisabler()){
var model = sitecoreService.GetItem<MyModel>("/sitecore/content/home");
}
查看 Glass 网站了解更多信息 - http://www.glass.lu/Mapper/Sc/Documentation/VersionCountDisabler
或者您可以全局禁用检查
protected void Application_BeginRequest()
{
Sitecore.Context.Items["Disable"] = new VersionCountDisabler();
}
关于c# - Sitecore GlassMapper 尝试使用查询属性获取没有版本的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32905017/