C# Sitecore 中的 GeneralLink

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19919954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:07:46  来源:igfitidea点击:

GeneralLink in Sitecore

c#asp.netsitecore

提问by Steve Ward

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).

我是 Sitecore 的新手。我创建了一个页面模板,并为类型为常规链接的 URL 添加了一个字段。我为链接的文本创建了另一个字段(这是本项目的标准做法)。

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");

采纳答案by jammykam

You should be careful using linkField.Urlsince 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.

您应该小心使用,linkField.Url因为它会错误地呈现指向 Sitecore 项目和媒体的内部链接。您应该改为使用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. Take a look this Sitecore Links with LinkManager and MediaManagerblog post which has the correct code you need for this.

根据链接的类型,最好有一个帮助器(扩展)方法为您返回正确的 url。看看这个Sitecore Links with 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 为您处理,但您似乎没有那个选项。

回答by Trayek

It'd be easier if you use the Link control:

如果您使用链接控件会更容易:

<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.

这样,您就不必执行任何代码隐藏操作,而且您还可以使用页面编辑器。

回答by Yogesh Sharma

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.

它会为你工作。

回答by HiralShah

You need to get the Linkfieldvalue of the item and than get the LinkFieldtype of that item. 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.

您需要获取项目的Linkfield价值,而不是获取该LinkField项目的类型。这将为您提供“内部”、“外部”、“mailto”的链接类型,并基于此可以获得链接字段的 url,正如@jammykam 所提到的。

Same thing you can do to retrieve the LinkTextas 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"];
}

回答by Josh Wheelock

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. Fill in that value then use:

当您为项目的 GeneralLink 字段分配值时,在弹出的内部链接对话框中会出现一个标有“链接描述”的字段。填写该值然后使用:

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

That's it. Everything is "wired-up" for you, auto-magically.

就是这样。一切都为您“自动连接”,自动神奇。

回答by KochFolie

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. The method makes it easy to output a valid URL no matter what type of link the field contains. For internal links, the method returns a URL from LinkManager.GetItemUrl(). For media links, the method returns a URL from MediaManager.GetMediaUrl(). For external links, anchor links, e-mail links, and JavaScript links, the method returns the value of the LinkField.Url property. (400051)

引入了新的 LinkField.GetFriendlyUrl() 方法。无论字段包含什么类型的链接,该方法都可以轻松输出有效的 URL。对于内部链接,该方法从 LinkManager.GetItemUrl() 返回一个 URL。对于媒体链接,该方法从 MediaManager.GetMediaUrl() 返回一个 URL。对于外部链接、锚链接、电子邮件链接和 JavaScript 链接,该方法返回 LinkField.Url 属性的值。(400051)

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

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