是否可以使用玻璃映射器检查Sitecore项目的模板ID?
我的业务逻辑将执行以下操作:
我想使用如下所述的SitecoreContext
类:http : //www.glass.lu/Mapper/Sc/Documentation/ISitecoreContext
我的代码如下所示:
var context = new SitecoreContext();
var currentItem = context.GetCurrentItem<MyModel>();
if(HasCorrectTemplate(currentItem))
{
return currentItem;
}
return GetFallbackItem();
我真的不想为此自定义Glass Mapper,因为在我看来,这应该是检查模板ID的基本功能。
我只能考虑为此使用某种棘手的查询,而没有找到有关其他可能性的文档。
您可以尝试使用:
[SitecoreType(EnforceTemplate = SitecoreEnforceTemplate.Template, TemplateId = "{ID}")]
public class MyModel
{
...
这是该EnforceTemplate
属性的描述:
/// <summary>
/// Forces Glass to do a template check and only returns an class if the item
/// matches the template ID or inherits a template with the templateId
///
/// </summary>
public SitecoreEnforceTemplate EnforceTemplate { get; set; }
EnforceTemplate
设置了属性后,Glass Mapper将检查要映射的项目是否与该SitecoreType
属性定义的模板的ID相匹配。如果是,则返回映射的项目,否则将其跳过。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句