问题描述
我是新来Sitecore的..我已经创建了一个模板,并添加一个字段类型一般链接的网址。我已经创造了另一个领域的文本链接(这是这个项目的标准做法)。
我只是想在我的用户控件中显示链接,但我只是不能得到它的工作。这应该是简单的,但林转圈圈
这里的code我试过的例子..
ASCX:
< ASP:超链接=服务器ID =lnkMain>< / ASP:超链接>
ascx.cs:
lnkMain.NavigateUrl = SiteCore.Context.Item.GetGeneralLink(链接1);
lnkMain.Text = item.GetFieldValue(Link1Text);
您应该小心使用 linkField.Url
,因为它会错误地渲染到Sitecore的项目和内部链接媒体。而应该使用 Sitecore.Links.LinkManager.GetItemUrl(项目)
和 Sitecore.Resources.Media.MediaManager.GetMediaUrl(项目)
那些。
这将是最好有一个帮手(扩展)方法返回给你正确的URL,基于链路的类型。看看这个<一个href=\"http://briancaos.word$p$pss.com/2012/08/24/sitecore-links-with-linkmanager-and-mediamanager/\">Sitecore与LinkManager和媒体管理博客文章c您需要为这个具有正确的$ C $链接。
有关参考:
公共静态字符串LinkUrl(此Sitecore.Data.Fields.LinkField LF)
{
开关(lf.LinkType.ToLower())
{
案内部:
//使用LinkMananger的内部联系,如果联系不为空
返回lf.TargetItem!= NULL? Sitecore.Links.LinkManager.GetItemUrl(lf.TargetItem)的String.Empty;
案传媒:
//使用媒体管理媒体的联系,如果联系不为空
返回lf.TargetItem!= NULL? Sitecore.Resources.Media.MediaManager.GetMediaUrl(lf.TargetItem)的String.Empty;
案外部:
//只是返回外部链接
返回lf.Url;
案锚:
// $ P $以#PFIX锚链接,如果链接如果不为空
回!string.IsNullOrEmpty(lf.Anchor)? #+ lf.Anchor:的String.Empty;
案电子邮件地址:
//只是返回邮寄地址链接
返回lf.Url;
案的JavaScript:
//只是返回的javascript
返回lf.Url;
默认情况下:
//只是请编译器,这
//条件将永远不会被满足
返回lf.Url;
}
}
用法:
Sitecore.Data.Fields.LinkField linkField = item.Fields [链接1];
lnkMain.NavigateUrl = linkField.LinkUrl();
这将是最好的,当然用&LT; SC:FieldRender&GT;
控件,让Sitecore的为您处理它,但它看起来像你没有这样的选择。
I'm new to Sitecore.. I have created a Page template and add a field for a URL of type General Link. I have created another field for the text for the link (this is standard practice in this project).
I simply want to display the link in my user control but I just cant get it to work. This should be simple but Im going round in circles
Here's an example of the code I've tried ..
ascx :
<asp:HyperLink runat="server" ID="lnkMain"></asp:HyperLink>
ascx.cs:
lnkMain.NavigateUrl = SiteCore.Context.Item.GetGeneralLink("Link1");
lnkMain.Text = item.GetFieldValue("Link1Text");
You should be careful using linkField.Url
since it it will incorrectly render internal links to Sitecore Items and Media. You should instead be using Sitecore.Links.LinkManager.GetItemUrl(item)
and Sitecore.Resources.Media.MediaManager.GetMediaUrl(item)
for those.
It would be better to have a helper (extension) method to return the correct url for you, based on the type of link. Take a look this Sitecore Links with LinkManager and MediaManager blog post which has the correct code you need for this.
For reference:
public static String LinkUrl(this 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;
}
}
Usage:
Sitecore.Data.Fields.LinkField linkField = item.Fields["Link1"];
lnkMain.NavigateUrl = linkField.LinkUrl();
It would be best of course to use <sc:FieldRender>
control and let Sitecore handle it for you, but it looks like you do not have that option.
这篇关于GeneralLink在Sitecore的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!