Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

c# - GeneralLink in 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");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...