asp.net – Glass Mapper打破了图像字段的标准值
发布时间:2020-12-16 04:06:53 所属栏目:asp.Net 来源:网络整理
导读:考虑模板: 公司 徽标(图像字段) 公司名称(文本字段) 公司模板在两个字段上都设置了标准值.如果我们获取公司项目并使用Glass保存而不进行任何更改,则Logo字段不再使用标准值. (公司名称字段未触及.) 看起来,问题是Glass.Mapper.Sc.DataMappers.SitecoreField
考虑模板:
公司 >徽标(图像字段) 公司模板在两个字段上都设置了标准值.如果我们获取公司项目并使用Glass保存而不进行任何更改,则Logo字段不再使用标准值. (公司名称字段未触及.) 看起来,问题是Glass.Mapper.Sc.DataMappers.SitecoreFieldImageMapper以不同于Sitecore的方式序列化该字段的值.当它试图保存时,它认为这是对字段的更改,不再使用标准值. 标准值: <image mediaid="{GUID}" /> 玻璃生成的价值: <image height="64" width="64" mediaid="{GUID}" alt="Alt text" /> 有没有办法让Glass生成与Sitecore相同的输出? 解决方法
我认为问题在于
SitecoreFieldImageMapper如何将ImageField映射到Image.为了获得高度,宽度和Alt使用公共属性.如果我们通过反射器查看它们,我们将看到它的值不直接来自字段:
public string Alt { get { string text = base.GetAttribute("alt"); if (text.Length == 0) { Item item = this.MediaItem; if (item != null) { MediaItem mediaItem = item; text = mediaItem.Alt; if (text.Length == 0) { text = item["Alt"]; } } } return text; } set { base.SetAttribute("alt",value); } } 如果字段不包含值(例如,对于“alt”:if(text.Length == 0)),则将从链接的MediaItem接收值.它导致在保存字段后从媒体库项添加高度,宽度和Alt. 要解决此问题,您可以尝试替换此代码: int height = 0; int.TryParse(field.Height,out height); int width = 0; int.TryParse(field.Width,out width); img.Alt = field.Alt; img.Height = height; img.Width = width; 直接获取属性而不是使用属性: int height = 0; if(int.TryParse(field.GetAttribute("height"),out height)) { img.Height = height; } int width = 0; if(int.TryParse(field.GetAttribute("width"),out width)) { img.Width = width; } img.Alt = field.GetAttribute("alt"); 使用Alt属性一切都应该没问题.但是宽度和高度可能存在问题,因为它们不是Nullable,我不确定GlassMapper如何处理没有设置的宽度和高度的图像. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – Azure服务架构与Docker一样吗?
- 白话ASP.NET MVC之二:Controller激活系统的概览
- asp.net – 无法加载类型’MyProject.MvcApplication’
- 在ASP.NET站点中将.Net 2.0与.Net 4.0混合使用
- 当我尝试添加映射文件时,ASP.NET脚手架的问题
- asp.net – 依赖注入和代码可维护性
- asp.net – 可以创建.config文件并将其包含到web.config中吗
- 如何从asp.net中的javascript调用codebehind函数?
- 配置 – ASP.NET Web Api:项目需要SQL Server Express
- asp.net – 是否有可能过滤SignalR中的接收器?