【问题标题】:Sitecore FieldRenderer output differences between CMS 6.5 and 7.0CMS 6.5 和 7.0 之间的 Sitecore FieldRenderer 输出差异
【发布时间】:2014-01-10 16:05:26
【问题描述】:

我最近一直在将内容和代码库从旧的 Sitecore CMS 6.5 安装(具有相当多的自定义)转移到全新安装的 CMS 7.0。

在原始站点中,Single-line text 字段已用于将任意 Javascript 呈现到页面中(用于跟踪)。这工作正常,Javascript 被渲染到页面中,未编码并按预期执行。

但是在新安装中,我注意到内容现在是 HTML 编码的,这意味着它不会在我正在使用的 Sitecore 7 安装下执行。

在我的子布局中,我有一个像这样的 FieldRenderer:

<sc:FieldRenderer ID="tracker" FieldName="Script" runat="server" />

我不确定是否有我尚未迁移的自定义/配置,或者这是否是 Sitecore 7 中引入的更改(可能出于安全原因)。

任何人都可以对此有所了解吗?我是否需要创建自己的字段类型以允许所需的行为,或者有什么方法可以使用“开箱即用”的字段类型来做到这一点?

【问题讨论】:

  • 也许Sitecore.Pipelines.RenderField.GetTextFieldValue piepline 是您的问题? stackoverflow.com/a/19257650/661447
  • @jammykam 谢谢会调查的
  • @jammykam 检查并且我使用默认的Sitecore.Pipelines.RenderField.GetTextFieldValueSitecore.Pipelines.RenderField.GetFieldValue 处理器,所以我仍然难倒
  • 这听起来像thisthis 问题,但假设您没有使用MVC。我会向 Sitecore 提出支持请求,如果您解决了问题,请发布答案。
  • @jammykam 谢谢,但不使用 MVC。将提出支持票,或者希望其他人可能有一个想法......

标签: c# .net sitecore sitecore7


【解决方案1】:

我检查了 Sitecore 6.5 和 7 中的 GetTextFieldValue 处理器类,看起来不同

这是来自 Sitecore 6.5:Sitecore.Pipelines.RenderField.GetTextFieldValue

 public void Process(RenderFieldArgs args)
   {
      Assert.ArgumentNotNull((object) args, "args");
      string fieldTypeKey = args.FieldTypeKey;
      if (fieldTypeKey != "text" && fieldTypeKey != "single-line text")
        return;
      args.WebEditParameters.Add("prevent-line-break", "true");
    }

这个来自 Sitecore 7:Sitecore.Pipelines.RenderField.GetTextFieldValue

 public void Process(RenderFieldArgs args)
{
  Assert.ArgumentNotNull((object) args, "args");
  string fieldTypeKey = args.FieldTypeKey;
  if (fieldTypeKey != "text" && fieldTypeKey != "single-line text")
    return;
  args.WebEditParameters.Add("prevent-line-break", "true");
  args.Result.FirstPart = HttpUtility.HtmlEncode(args.Result.FirstPart);
}

您可以在 Sitecore 7 的 Process 方法的最后一行代码中看到结果已编码。 您可以为 GetTextField 处理器创建自己的类并将其添加到 RenderField 管道,但我建议您将字段从 Single Line Text 更改为 Multi Line Text 或 Memo Field 。

我在 Sitecore 6.5 和 7 上检查了 Sitecore.Pipelines.RenderField.GetMemoFieldValue 类,实现相同,结果未编码:

namespace Sitecore.Pipelines.RenderField
{
  /// <summary>
  /// Implements the RenderField.
  /// 
  /// </summary>
  public class GetMemoFieldValue
  {
    /// <summary>
    /// Gets the field value.
    /// 
    /// </summary>
    /// <param name="args">The arguments.</param>
    public void Process(RenderFieldArgs args)
    {
      string fieldTypeKey = args.FieldTypeKey;
      if (fieldTypeKey != "memo" && fieldTypeKey != "multi-line text")
        return;
      string linebreaks = args.RenderParameters["linebreaks"];
      if (linebreaks == null)
        return;
      args.Result.FirstPart = GetMemoFieldValue.Replace(args.Result.FirstPart, linebreaks);
      args.Result.LastPart = GetMemoFieldValue.Replace(args.Result.LastPart, linebreaks);
      args.WebEditParameters.Add("linebreak", "br");
    }

    /// <summary>
    /// Replaces the specified linebreaks.
    /// 
    /// </summary>
    /// <param name="linebreaks">The linebreaks.</param><param name="output">The output.</param>
    /// <returns>
    /// The replace.
    /// </returns>
    private static string Replace(string output, string linebreaks)
    {
      output = output.Replace("\r\n", linebreaks);
      output = output.Replace("\n\r", linebreaks);
      output = output.Replace("\n", linebreaks);
      output = output.Replace("\r", linebreaks);
      return output;
    }
  }
}

GetTextField 的代码已在 Sitecore 6.6 Update 3 上更新,您可以在 release history 上查看:

页面编辑器在 6.6 Update-3 中,修改了管道 在呈现单行文本字段时对字段值进行 HTML 编码 (参考号 327905)。这在页面编辑器中无法正常工作 显示编码值。如果用户保存了页面, 已经编码的值将再次被 HTML 编码。 (384997)

希望对你有帮助。

【讨论】:

  • 这看起来完全解释了这个问题。我还使用反射器检查了 2 个程序集,但不知何故错过了较新程序集中的 HtmlEncoding。我也没有注意到详细说明更改的更新。直到明天我才能确定,但​​届时会接受你的(非常彻底的)回答。非常感谢!
  • 你知道 HTML 只对单行字段类型进行编码,而对备忘录和多行字段不进行编码的原因吗?他们没有将更改应用于所有字段,这似乎很奇怪。
  • 我无法准确回答它们为什么会改变,但我认为 memo 和 multi line 没有改变,因为对于 html 它具有编码的富文本字段。
  • 我转而使用 multi-line 字段,因为 memo 已弃用。
【解决方案2】:

似乎很多人对这篇文章感到困惑,并开始要求越来越多的人来解决上述问题。

FieldRenderer 中似乎存在阻止呈现 HTML 标记的错误 Sitecore CMS 不进行 HTML 编码

Sitecore CMS 在 6.6 修订版之前呈现 单行文本 字段和 链接 字段时没有对字段值进行 HTML 编码。 131111(6.6 更新 3)。由于此问题,从 单行文本链接 字段输出值的页面未通过 W3C 标记验证。

根据 Sitecore CMS 发行说明,此问题自 Sitecore CMS 6.6 rev. 起已得到解决。 131111(又名 Update-3)

布局和渲染

  • 管道没有 HTML 渲染单行文本字段时对字段值进行编码 链接字段。 (327905) 注意:渲染时对字段值进行编码 链接字段原来有意想不到的副作用。场 因此,链接字段的值不再在 6.6 Update-5 中编码 及以后(参考号 382059)。 [2013 年 4 月 17 日添加]
  • LinkRenderer 类未对标题属性的值进行编码 渲染链接字段时。 (327905、347361) [1 月 15 日更新, 2014]

从 Sitecore CMS 6.6.0 rev 开始,它得到了改进。 130404(又名 Update-5)

布局和渲染

  • 在 6.6 Update-3 中,管道已修改为在呈现链接字段时对字段值进行 HTML 编码(参考编号 327905)。事实证明,这会产生意想不到的副作用,例如在链接字段中嵌入 IMG 时。因此,链接字段的字段值在 6.6 Update-5 及更高版本中不再编码。 (382059)

自 CMS 6.6.0 rev. 130529(又名 Service Pack-1)

页面编辑器

  • 在 6.6 Update-3 中,管道已修改为在呈现单行文本字段时对字段值进行 HTML 编码(参考编号 327905)。这在显示编码值的页面编辑器中无法正常工作。如果用户保存了页面,已经编码的值将再次被 HTML 编码。 (384997)

从 6.6 版本开始。 131111 (6.6 Update-3) 及更高版本 Sitecore CMS HTML 使用HttpUtility.HtmlEncode 方法对单行文本 字段和链接 字段的值进行编码。

已在 GetTextValue 处理器以及 Sitecore.Xml.Xsl.LinkRenderer 类中引入了更改。

<renderField>
  ...
  <processor type="Sitecore.Pipelines.RenderField.GetTextFieldValue, Sitecore.Kernel" />
  ...
</renderField>

您可以联系支持人员,他们知道问题所在,并且 有可用的修复程序。您应该询问 Sitecore.Support.381846.dll

Sitecore.Support.381846 程序集已修改 GetTextFieldValue 处理器,其中缺少以下代码行。

args.Result.FirstPart = HttpUtility.HtmlEncode(args.Result.FirstPart);

这个“修复”只是带来了 单行文本 字段的旧行为。换句话说,它引入了参考编号为#327905 的旧问题(详见上文)。

为什么在升级到 Sitecore CMS 6.6 Update-3 及更高版本后,我的 Sitecore 解决方案中的一切都出现了问题?因为您错误地使用了 单行文本 字段。此字段类型不假定存储 HTML 或 JavaScript。这是存储内容值的内容字段类型。如果您在内容字段中存储 HTML 标记和 JavaScript,则意味着您没有遵循 Sitecore 最佳实践。

总结:

  1. 参考号 #381846 没有问题 (Sitecore.Support.381846.dll) 在 Sitecore CMS 中。
  2. 自 Sitecore CMS 6.6 Update-3 以来,实际问题 #327905 已得到解决。
  3. Sitecore HTML 编码单行文本链接字段类型。
  4. 不要在 单行文本 等内容字段中存储 HTML 和 Java Script 或任何与演示相关的元数据。否则,内容编辑器很有可能会破坏整个 如果在 单行文本 中错误地更改了 HTML/JavaScript,则页面展示 字段。
  5. 内容及其呈现方式必须分开存储。

我希望这有助于说清楚。

最好的祝愿, 奥列格·布罗夫

更新 1:

Sitecore CMS 和 DMS 7.2 修订版。 141226(又名 7.2 Update-3)在 Web.config 文件中引入了 Rendering.HtmlEncodedFieldTypes 设置,该设置指定了一个以管道分隔的字段类型列表,当由 &lt;renderField&gt; 管道呈现时,这些字段类型应进行 HTML 编码。

<!--  RENDERING - HTML ENCODED FIELD TYPES
      Specifies a pipe-separated list of field types that should be HTML encoded when rendered by the <renderField> pipeline.
      Default value: text|single-line text
-->
<setting name="Rendering.HtmlEncodedFieldTypes" value="text|single-line text" />

现在您可以控制哪些 Sitecore 字段类型应该或不应该是 HTML 编码的。

【讨论】:

    【解决方案3】:

    从 Sitecore 6.5 升级到 7.0 时,我们遇到了同样的问题。 FieldRenderer 中似乎存在阻止呈现 HTML 标记的错误。您可以与支持人员联系,他们知道问题并有可用的修复程序。

    您应该询问需要像这样集成的 Sitecore.Support.381846.dll:

    在 web.config 中替换这一行

    <processor type="Sitecore.Pipelines.RenderField.GetTextFieldValue, Sitecore.Kernel" />
    

    有了这个

    <processor type="Sitecore.Support.Pipelines.RenderField.GetTextFieldValue, Sitecore.Support.381846" />
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢。今天晚些时候我可以检查一下
    【解决方案4】:

    我们遇到了这个问题并找到了这篇文章。我们选择通过直接进入数据库并使用 nbsp 和 amp 更新单个文本字段来处理它,这些是我们的主要问题区域,可以针对您正在处理的每个编码字符重复这些步骤。我们选择手动处理链接字段。

    通过在 Master 和 web 中运行替换,您可以省略重新发布字段。

    问候 一月

    SELECT TOP 18000 * FROM [uat_Sitecore_master_new].[dbo].[VersionedFields] where (Value like '%&%;%' ) 和 FieldId(从 SharedFields 中选择 ItemId,其中的值类似于“单行文本”) --and FieldId in (SElect ItemId FROM SharedFields where Value like 'General Link')

    /* 查找所有具有 NBSP 的单行文本字段 / SELECT Replace(Value, ' ',' '), FROM [uat_Sitecore_master_new].[dbo].[VersionedFields] where (Value like '% %') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    /* 替换单行文本字段中的 NBSP */ UPDATE [uat_Sitecore_master_new].[dbo].[VersionedFields] SET Value = Replace(Value, ' ',' ') where (Value like '% %') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    UPDATE [uat_Sitecore_web_new].[dbo].[VersionedFields] SET Value = Replace(Value, ' ',' ') where (Value like '% %') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    /* 查找所有具有 AMP 的单行文本字段 /
    SELECT Replace(Value, '&','&'),
    FROM [uat_Sitecore_master_new].[dbo].[VersionedFields] where (Value like '%&%' ) 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    /* 替换单行文本字段中的 AMP */
    UPDATE [uat_Sitecore_master_new].[dbo].[VersionedFields] SET Value = Replace(Value, '&','&') where (Value like '%&%') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    UPDATE [uat_Sitecore_web_new].[dbo].[VersionedFields] SET Value = Replace(Value, '&','&') where (Value like '%&%') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    /* 查找所有包含“”的单行文本字段 /
    SELECT Replace(Value, ''',''''),
    FROM [uat_Sitecore_master_new].[dbo].[VersionedFields] where (Value like '%'%' ) 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    /* 在单行文本字段中替换“” */
    UPDATE [uat_Sitecore_master_new].[dbo].[VersionedFields] SET Value = Replace(Value, ''','''') where (Value like '%'%') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    UPDATE [uat_Sitecore_web_new].[dbo].[VersionedFields] SET Value = Replace(Value, ''','''') where (Value like '%'%') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    /* 查找所有带有“”的单行文本字段 /
    SELECT Replace(Value, '"','"'),
    FROM [uat_Sitecore_master_new].[dbo].[VersionedFields] where (Value like '%"%' ) 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    /* 在单行文本字段中替换“” */
    UPDATE [uat_Sitecore_master_new].[dbo].[VersionedFields] SET Value = Replace(Value, '"','"') where (Value like '%"%') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    UPDATE [uat_Sitecore_web_new].[dbo].[VersionedFields] SET Value = Replace(Value, '"','"') where (Value like '%"%') 和 FieldId in (SElect ItemId FROM SharedFields where Value like 'Single-Line Text')

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      相关资源
      最近更新 更多