我在我的项目解决方案中得到玻璃映射器异常,如下
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:IDPNowTrunksrcIELTS.WebIELTSControllersRedirectionController.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重定向= context.GetCurrentItem()中得到异常;线。
您的字段值包含'PermanentRedirect',但您的enum没有该值。它有:"永久"one_answers"临时"。
名称必须精确匹配(区分大小写)。也许你在某个时间点改变了它?或者你有一个错误的默认值?