Glass Mapper异常:无法将值PermanentRedirect转换为枚举类型

时间:2015-07-21 03:32:50

标签: enums sitecore aero-glass glass-mapper

我在项目解决方案中获得了玻璃映射器异常,如下所示

stack trace:
[MapperException: Can not convert value PermanentRedirect to enum type IELTS.Web.IELTS.Models.Redirections.StatusCodes]
Glass.Mapper.Sc.DataMappers.SitecoreFieldEnumMapper.GetFieldValue(String fieldValue, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) +280
Glass.Mapper.Sc.DataMappers.AbstractSitecoreFieldMapper.GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) +124

[MapperException: Failed to map field Type with value PermanentRedirect]
Glass.Mapper.Sc.DataMappers.AbstractSitecoreFieldMapper.GetField(Field field, SitecoreFieldConfiguration config, SitecoreDataMappingContext context) +326
Glass.Mapper.AbstractDataMapper.MapCmsToProperty(AbstractDataMappingContext mappingContext) +29
Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) +158

[MapperException: Failed to map property Type on IELTS.Web.IELTS.Models.Redirections.Redirection]
Glass.Mapper.Configuration.AbstractTypeConfiguration.MapPropertiesToObject(Object obj, IAbstractService service, AbstractTypeCreationContext context) +488
Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.CreateObject(ObjectConstructionArgs args) +295
Glass.Mapper.Pipelines.ObjectConstruction.Tasks.CreateConcrete.CreateConcreteTask.Execute(ObjectConstructionArgs args) +215
Glass.Mapper.Pipelines.AbstractPipelineRunner`2.Run(T args) +225
Glass.Mapper.AbstractService.InstantiateObject(AbstractTypeCreationContext abstractTypeCreationContext) +170
Glass.Mapper.Sc.SitecoreService.CreateType(Type type, Item item, Boolean isLazy, Boolean inferType, Object[] constructorParameters) +265
Glass.Mapper.Sc.SitecoreService.CreateType(Item item, Boolean isLazy, Boolean inferType) +109
Glass.Mapper.Sc.SitecoreContext.GetCurrentItem(Boolean isLazy, Boolean inferType) +90

IELTS.Web.IELTS.Controllers.RedirectionController.Redirect() in c:\IDPNow\Trunk\src\IELTS.Web\IELTS\Controllers\RedirectionController.cs:15
lambda_method(Closure , ControllerBase , Object[] ) +79

System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +261
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +124
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +681242
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +681242
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +307
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +345

我的渲染类如下所示,

 public class Redirection : RenderingModel
    {
        [SitecoreField("Destination")]
        public virtual Glass.Mapper.Sc.Fields.Link Destination { get; set; }

        [SitecoreField("Type")]
        public virtual StatusCodes Type { get; set; }
    }

    public enum StatusCodes
    {
        Permanent = 301,
        Temporary = 302
    }

我的控制器方法如下,

public ActionResult Redirect()
        {
            var context = new SitecoreContext(IdpTemplates.CurrentContext);

            var redirection = context.GetCurrentItem<Redirection>();

            return new RedirectResult(redirection.Destination.Url, Convert.ToInt32(redirection.Type) == 301);
        }

在上面的方法中我在var redirection = context.GetCurrentItem();线。

1 个答案:

答案 0 :(得分:0)

您的fieldvalue包含'PermanentRedirect',但您的enum没有该值。它有:'永久'和'临时'。

名称必须完全匹配(区分大小写)。也许你在某个时间点改变了它?或者您的默认值可能不正确?