简体   繁体   English

Sitecore中的GeneralLink

[英]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. 我是Sitecore的新手。我创建了一个页面模板,并为General Link类型的URL添加了一个字段。 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 : ascx:

<asp:HyperLink runat="server" ID="lnkMain"></asp:HyperLink>

ascx.cs: 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. 您应该小心使用linkField.Url因为它会错误地呈现Sitecore项目和媒体的内部链接。 You should instead be using Sitecore.Links.LinkManager.GetItemUrl(item) and Sitecore.Resources.Media.MediaManager.GetMediaUrl(item) for those. 您应该使用Sitecore.Links.LinkManager.GetItemUrl(item)Sitecore.Resources.Media.MediaManager.GetMediaUrl(item)

It would be better to have a helper (extension) method to return the correct url for you, based on the type of link. 根据链接类型,最好有一个帮助(扩展)方法为您返回正确的URL。 Take a look this Sitecore Links with LinkManager and MediaManager blog post which has the correct code you need for this. 看看这个Sitecore链接与LinkManager和MediaManager博客文章,其中包含您需要的正确代码。

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. 当然最好使用<sc:FieldRender>控件让Sitecore为你处理它,但看起来你没有那个选项。

As of Sitecore 7.2 there is an alternative to linkField.Url: 从Sitecore 7.2开始,有一个替代linkField.Url:

Sitecore.Data.Fields.LinkField linkField = item.Fields["Link1"];
lnkMain.NavigateUrl = linkfield.GetFriendlyUrl();

A new LinkField.GetFriendlyUrl() method has been introduced. 引入了一个新的LinkField.GetFriendlyUrl()方法。 The method makes it easy to output a valid URL no matter what type of link the field contains. 无论字段包含哪种类型的链接,该方法都可以轻松输出有效的URL。 For internal links, the method returns a URL from LinkManager.GetItemUrl(). 对于内部链接,该方法从LinkManager.GetItemUrl()返回一个URL。 For media links, the method returns a URL from MediaManager.GetMediaUrl(). 对于媒体链接,该方法从MediaManager.GetMediaUrl()返回一个URL。 For external links, anchor links, e-mail links, and JavaScript links, the method returns the value of the LinkField.Url property. 对于外部链接,锚链接,电子邮件链接和JavaScript链接,该方法返回LinkField.Url属性的值。 (400051) (400051)

http://techitpro.com/uncategorized/sitecore-7-2-changes/ http://techitpro.com/uncategorized/sitecore-7-2-changes/

It'd be easier if you use the Link control: 如果使用Link控件会更容易:

<sc:Link Field="Link1" runat="server" ID="link">
    <sc:Text Field="Link1Text" runat="server" ID="linkText" />
</sc:Link>

That way, you don't have to do any code-behind stuff and you'll be able to use the Page Editor as well. 这样,您不必执行任何代码隐藏的东西,您也可以使用页面编辑器。

You can use below 你可以在下面使用

<sc:Link ID="scLink" runat="server" Field="Your Link Field Name">
    <sc:FieldRenderer ID="frTest" runat="server" FieldName="Your Text Field Name" />
</sc:Link>

It will work for you. 它会对你有用。

You need to get the Linkfield value of the item and than get the LinkField type of that item. 您需要获取项目的Linkfield值,然后获取该项目的LinkField类型。 This will give you the type of link either an "Internal", "external", "mailto" and based on that can get the url of the link field as this is mentioned by @jammykam. 这将为您提供“内部”,“外部”,“mailto”链接的类型,并且基于该链接可以获取链接字段的URL,因为@jammykam提到了这一点。

Same thing you can do to retrieve the LinkText as well. 您也可以检索LinkText

For Reference : 以供参考 :

public static string GetGeneralLinkText(LinkField link)
{
    text = "";

    if (link == null)
        return false;

    if (!string.IsNullOrEmpty(link.Text))
    {
        text = link.Text;
        return true;
    }

    switch (link.LinkType)
    {
        case "internal":
            if (link.TargetItem == null)
                return false;
            text = link["Text Field Name"];
            break;
        case "external":
        case "mailto":
        case "anchor":
        case "javascript":
            text = link.Text;
            break;
        case "media":
            if (link.TargetItem == null)
                return false;
            Sitecore.Data.Items.MediaItem media = new Sitecore.Data.Items.MediaItem(link.TargetItem);
            text = media.Name;
            break;
        case "":
            break;
        default:
            return "";
    }

    return  link["Text Field Name"];
}

When you assign a value to a GeneralLink field of an item there is a field labeled "Link Description" in the Internal Link dialog that pops up. 为项目的GeneralLink字段分配值时,弹出的“内部链接”对话框中会出现标记为“链接描述”的字段。 Fill in that value then use: 填写该值然后使用:

<sc:Link runat="server" Field="{YourFieldName}" />

That's it. 而已。 Everything is "wired-up" for you, auto-magically. 所有东西都是“自动连线”,自动神奇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM