Glass Mapper(4.1.2.67)无法映射富文本字段

时间:2016-08-18 07:52:04

标签: sitecore asp.net-web-api2 sitecore7 glass-mapper

我已经定义了几个类来映射模板项,如下所示:

public class ContentBase
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    [SitecoreParent]
    public virtual ContentBase Parent { get; set; }
    [SitecoreItem]
    public virtual Item Self { get; set; }
}
[SitecoreType(TemplateId = "{7979766D-DB9C-4E75-9BE3-5B481C6AB6FF}", AutoMap = true)]
public class EventsListing : ContentBase
{
    [SitecoreField(FieldName = "EventsLocation")]
    public virtual SitecoreFolder<Event> Events { get; set; }
}
[SitecoreType(TemplateId = "{CED01C9B-6284-461A-848F-2CDD00CC6DEB}", AutoMap = true)]
public class Event : ContentBase
{
    public virtual string Title { get; set; }
    public virtual string Details { get; set; }
    public virtual string iCalSummary { get; set; }
    public virtual Image ImageLandscape { get; set; }
    public virtual Image ImagePortrait { get; set; }
    public virtual Image ImageSquare { get; set; }
    public virtual string Date { get; set; }
    public virtual DateTime DateStart { get; set; }
    public virtual DateTime DateEnd { get; set; }
    public virtual string Location { get; set; }
    public virtual string GoogleMapsAddress { get; set; }
    public virtual string MemberDiscount { get; set; }
    public virtual Link EventLinkUrl { get; set; }
    public virtual string EventLinkText { get; set; }
}

活动的模板

The template for the Event

我有一个基于GlassUserControl的EventsListing的子布局,并成功获取了EventsListing和子事件的所有字段。然后,我在Date上有一个链接,通过web.api控制器为事件生成iCal

    [RoutePrefix("hbf/api/ical")]
public class EventICalController : ApiController
{
    [Route("{id:guid}")]
    [HttpGet]
    public HttpResponseMessage Get(Guid id)
    {
        var scc = new SitecoreContext(); 
        var item = scc.GetItem<Item>(id);
        var myEvent = scc.GetItem<Models.Event>(id);

该项目看起来很好,我可以访问这些字段;但是调用scc.GetItem(id)会在“Details”字段上抛出异常。

异常嵌套是(下面的完整堆栈跟踪): Glass.Mapper.MapperException“无法创建类型Models.Event” Glass.Mapper.MapperException“无法在/ sitecore / content / ...上映射属性” Glass.Mapper.MapperException“无法映射Models.Event上的属性详细信息” System.NullReferenceException

如果我注释掉“详细信息”属性,它就会起作用。

我已经尝试了各种方法来获取SitecoreContext,甚至指定语言无济于事。我还尝试使用名称和/或ID以及字段类型设置SitecoreField属性。

我可以在配置(或其他)方面解决这个问题吗?

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "Failed to create type xxx.Web.Models.Event",
  "ExceptionType": "Glass.Mapper.MapperException",
  "StackTrace": "   at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.CreateObject(ObjectConstructionArgs args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\ObjectConstruction\\Tasks\\CreateConcrete\\CreateConcreteTask.cs:line 115
       at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.Execute(ObjectConstructionArgs args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\ObjectConstruction\\Tasks\\CreateConcrete\\CreateConcreteTask.cs:line 68
       at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 77
       at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82
       at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82
       at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82
       at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82
       at Glass.Mapper.Pipelines.AbstractPipelineRunner`2.<>c__DisplayClass3.<CreateTaskExpression>b__2(T args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\AbstractPipelineRunner.cs:line 82
       at Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractService.cs:line 138
       at Glass.Mapper.Sc.SitecoreService.CreateType(Type type, Item item, Boolean isLazy, Boolean inferType, Dictionary`2 parameters, Object[] constructorParameters) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\SitecoreService.cs:line 498
       at Glass.Mapper.Sc.SitecoreService.GetItem[T](Guid id, Boolean isLazy, Boolean inferType) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\SitecoreService.cs:line 899
       at xxx.Web.Services.xxx.EventICalController.Get(Guid id) in d:\\Dev\\LW\\xxx.Web\\Services\\xxx\\EventICalController.cs:line 57
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
        --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()
        --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()",
  "InnerException": {
    "Message": "An error has occurred.",
    "ExceptionMessage": "Failed to map properties on /sitecore/content/xxx/Home/Living well/Events/datasources/Health and fitness events/xxx Fitness.",
    "ExceptionType": "Glass.Mapper.MapperException",
    "StackTrace": "   at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 174
           at Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.CreateObject(ObjectConstructionArgs args) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Pipelines\\ObjectConstruction\\Tasks\\CreateConcrete\\CreateConcreteTask.cs:line 104",
    "InnerException": {
      "Message": "An error has occurred.",
      "ExceptionMessage": "Failed to map property Details on xxx.Web.Models.Event",
      "ExceptionType": "Glass.Mapper.MapperException",
      "StackTrace": "   at Glass.Mapper.Configuration.AbstractTypeConfiguration.<>c__DisplayClassb.<CreatePropertyExpression>b__a(Object obj, AbstractDataMappingContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 123
           at Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 144",
      "InnerException": {
        "Message": "An error has occurred.",
        "ExceptionMessage": "Failed to map to property 'Details' on type 'xxx.Web.Models.Event'",
        "ExceptionType": "Glass.Mapper.MapperException",
        "StackTrace": "   at Glass.Mapper.AbstractDataMapper.MapCmsToProperty(AbstractDataMappingContext mappingContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractDataMapper.cs:line 64
               at Glass.Mapper.Configuration.AbstractTypeConfiguration.<>c__DisplayClassb.<CreatePropertyExpression>b__a(Object obj, AbstractDataMappingContext context) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\Configuration\\AbstractTypeConfiguration.cs:line 119",
        "InnerException": {
          "Message": "An error has occurred.",
          "ExceptionMessage": "Object reference not set to an instance of an object.",
          "ExceptionType": "System.NullReferenceException",
          "StackTrace": "   at xxx.Core.Pipelines.RenderField.GetDevModeContent.Process(RenderFieldArgs args)
               at (Object , Object[] )
               at Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args)
               at Glass.Mapper.Sc.DataMappers.SitecoreFieldStringMapper.RunPipeline(Field field) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper.Sc\\DataMappers\\SitecoreFieldStringMapper.cs:line 99
               at Glass.Mapper.AbstractDataMapper.MapCmsToProperty(AbstractDataMappingContext mappingContext) in c:\\TeamCity\\buildAgent\\work\\8567e2ba106d3992\\Source\\Glass.Mapper\\AbstractDataMapper.cs:line 60"
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:2)

我将Glass.Mapper.Sc。*项目源添加到我的解决方案( yay for open source )并逐步完成,发现我需要添加一些额外的富文本字段的配置如下:

[SitecoreField(Setting = SitecoreFieldSettings.RichTextRaw)]
public virtual string Details { get; set; }

这样,字段不会通过渲染过程而只返回原始HTML,这就是我想要的。这是渲染过程失败,这是一个半答案,因为我不知道为什么渲染过程失败,但我不需要它。不幸的是,我没有在Glass.Mapper文档中找到我需要的东西。

答案 1 :(得分:0)

我相信Glass无法解析Context.Site。由于web api调用默认情况下没有上下文站点。您可以将整个呼叫包裹在using (new SiteContextSwitcher(Factory.GetSite("yoursite")))内,或者找到另一种方法在服务呼叫中设置Context.Site。

或者如果您有多个网站,请确保他们在hostName节点中定义了<sites>属性,并在通话顶部显示如下内容:

var sites = Sitecore.Configuration.Factory.GetSiteInfoList();
string currentHost = HttpContext.Current.Request.Url.Host;
var currentSite = sites.FirstOrDefault(obj => obj.HostName.Equals(currentHost, StringComparison.InvariantCultureIgnoreCase));
if (currentSite != null)
   {
       var newSite = new Sitecore.Sites.SiteContext(currentSite);
       if (newSite != null)
       {                 
           using (new SiteContextSwitcher(newSite))
           {

                ///Code here
           }
       {
    {