如何在 Sitecore 7.2 C# 中获取一般 link 的 url 路径
How to get the url path for a general link in Sitecore 7.2 C#
我对 Sitecore 和 C# 很陌生,过去几天我一直在努力解决一些问题,所以我希望你们中的一个能帮助我。
我刚刚从 Sitecore 6 升级到 7,并且发生了一些变化。以下代码曾经在 Sitecore 6 中工作,但您获取 LinkField 的 url 的方式不再有效。
protected string writeBalloon(Item targetItem)
{
string balloonString = "";
Sitecore.Data.Fields.LinkField linkfield = targetItem.Fields["Link"];
balloonString += "<a class='balloon-link' href='" + linkfield.Url + "'>";
balloonString += "<div class='balloon'>";
balloonString += "<h3>";
balloonString += targetItem.Fields["Title"];
balloonString += "</h3>";
balloonString += "<p>";
balloonString += targetItem.Fields["Text"];
balloonString += "</p>";
balloonString += "</div>";
balloonString += "</a>";
return balloonString;
}
我想我 read/tried google 上关于这件事的所有文章,但我似乎还是没弄对。
您需要先检查您的 link 字段类型,使用 linkfield.Url 仅适用于 sitecore 中的外部 link 类型,这里是您的代码应该是什么样子:
public String LinkUrl(Sitecore.Data.Fields.LinkField lf)
{
switch (lf.LinkType.ToLower())
{
case "internal":
// Use LinkMananger for internal links, if link is not empty
return lf.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(lf.TargetItem) : string.Empty;
case "media":
// Use MediaManager for media links, if link is not empty
return lf.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(lf.TargetItem) : string.Empty;
case "external":
// Just return external links
return lf.Url;
case "anchor":
// Prefix anchor link with # if link if not empty
return !string.IsNullOrEmpty(lf.Anchor) ? "#" + lf.Anchor : string.Empty;
case "mailto":
// Just return mailto link
return lf.Url;
case "javascript":
// Just return javascript
return lf.Url;
default:
// Just please the compiler, this
// condition will never be met
return lf.Url;
}
}
protected string writeBalloon(Item targetItem)
{
string balloonString = "";
Sitecore.Data.Fields.LinkField linkfield = targetItem.Fields["Link"];
balloonString += "<a class='balloon-link' href='" + LinkUrl(linkfield) + "'>";
balloonString += "<div class='balloon'>";
balloonString += "<h3>";
balloonString += targetItem.Fields["Title"];
balloonString += "</h3>";
balloonString += "<p>";
balloonString += targetItem.Fields["Text"];
balloonString += "</p>";
balloonString += "</div>";
balloonString += "</a>";
return balloonString;
}
Note
:LinkUrl() 方法的代码片段取自此 Answer
从 Sitecore 7.2 开始,在 LinkField
class 中添加了一个方法 GetFriendlyUrl()
来解决这个问题。
Sitecore.Data.Fields.LinkField linkfield = targetItem.Fields["Link"];
var url = linkfield.GetFriendlyUrl();
见GeneralLink in Sitecore
我对 Sitecore 和 C# 很陌生,过去几天我一直在努力解决一些问题,所以我希望你们中的一个能帮助我。
我刚刚从 Sitecore 6 升级到 7,并且发生了一些变化。以下代码曾经在 Sitecore 6 中工作,但您获取 LinkField 的 url 的方式不再有效。
protected string writeBalloon(Item targetItem)
{
string balloonString = "";
Sitecore.Data.Fields.LinkField linkfield = targetItem.Fields["Link"];
balloonString += "<a class='balloon-link' href='" + linkfield.Url + "'>";
balloonString += "<div class='balloon'>";
balloonString += "<h3>";
balloonString += targetItem.Fields["Title"];
balloonString += "</h3>";
balloonString += "<p>";
balloonString += targetItem.Fields["Text"];
balloonString += "</p>";
balloonString += "</div>";
balloonString += "</a>";
return balloonString;
}
我想我 read/tried google 上关于这件事的所有文章,但我似乎还是没弄对。
您需要先检查您的 link 字段类型,使用 linkfield.Url 仅适用于 sitecore 中的外部 link 类型,这里是您的代码应该是什么样子:
public String LinkUrl(Sitecore.Data.Fields.LinkField lf)
{
switch (lf.LinkType.ToLower())
{
case "internal":
// Use LinkMananger for internal links, if link is not empty
return lf.TargetItem != null ? Sitecore.Links.LinkManager.GetItemUrl(lf.TargetItem) : string.Empty;
case "media":
// Use MediaManager for media links, if link is not empty
return lf.TargetItem != null ? Sitecore.Resources.Media.MediaManager.GetMediaUrl(lf.TargetItem) : string.Empty;
case "external":
// Just return external links
return lf.Url;
case "anchor":
// Prefix anchor link with # if link if not empty
return !string.IsNullOrEmpty(lf.Anchor) ? "#" + lf.Anchor : string.Empty;
case "mailto":
// Just return mailto link
return lf.Url;
case "javascript":
// Just return javascript
return lf.Url;
default:
// Just please the compiler, this
// condition will never be met
return lf.Url;
}
}
protected string writeBalloon(Item targetItem)
{
string balloonString = "";
Sitecore.Data.Fields.LinkField linkfield = targetItem.Fields["Link"];
balloonString += "<a class='balloon-link' href='" + LinkUrl(linkfield) + "'>";
balloonString += "<div class='balloon'>";
balloonString += "<h3>";
balloonString += targetItem.Fields["Title"];
balloonString += "</h3>";
balloonString += "<p>";
balloonString += targetItem.Fields["Text"];
balloonString += "</p>";
balloonString += "</div>";
balloonString += "</a>";
return balloonString;
}
Note
:LinkUrl() 方法的代码片段取自此 Answer
从 Sitecore 7.2 开始,在 LinkField
class 中添加了一个方法 GetFriendlyUrl()
来解决这个问题。
Sitecore.Data.Fields.LinkField linkfield = targetItem.Fields["Link"];
var url = linkfield.GetFriendlyUrl();
见GeneralLink in Sitecore