Sitecore link url 编码 (XSLT)

Sitecore link url encoding (XSLT)

我正在使用以下代码在 xslt 中生成指向我的项目的链接:

<sc:link select=".">
  <xsl:value-of select="sc:fld('SomeField',.)" />
</sc:link>

不幸的是,当项目名称中包含空格而不是 %20 时,生成的 URL 包含空格。 我希望有一些我可以调整的设置来解决这个问题。有人知道解决方案吗?

一种解决方案是使用 Sitecores encodeNameReplacers。

在 web.config 中找到该部分并添加:

<replace mode="on" find=" " replaceWith="-" /> 

更多信息在这里:http://sitecoreninja.blogspot.co.uk/2013/03/replace-space-with-dash-in-url.html

只需使用"translate"函数:

<sc:link select=".">
    <xsl:value-of select="sc:fld('SomeField',translate(., ' ', '-'))"/>
</sc:link>

回答我自己的问题,因为我比 Mark 的解决方案更喜欢我的解决方案。

我已将 public 静态方法 public static string UrlPathEncode(string) 添加到作为 xsl 扩展添加的 class class:

public static string UrlPathEncode(string url)
{
    return HttpUtility.UrlPathEncode(url);
}

将此添加到 web.config:

<xslExtensions>
  <extension mode="on" type="MyProject.Xsl.XslHelper, MyProject"
    namespace="http://www.myproject.net/sc" singleInstance="true" />
  ..

添加对 xslt 文件的引用:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:mp="http://www.myproject.net/sc"
  ..

现在您可以像这样使用 UrlPathEncode 函数:

<a href="{mp:UrlPathEncode(sc:path(.))}">
  <xsl:value-of select="sc:fld('SomeField',.)" />
</a>

这不是一个真正优雅的解决方案,但它确实对 URL 路径中不允许的每个字符进行了编码。