You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Episerver 7中如何禁用LinkItemCollection外部URL转内部页面功能?

If you need to turn an external-style URL (like https://yourdomain.com/about-us) into an internal Episerver content reference within a LinkItem, here's a practical approach:

  • First, validate that the URL belongs to your site (strip the domain and check against your site's base URL).
  • Use ContentLoader to locate the corresponding content item using the URL's relative path.
  • Update the LinkItem to use the internal ContentLink instead of the external Href.

Here’s a code snippet to implement this:

using EPiServer.Core;
using EPiServer.Web;
using EPiServer.ServiceLocation;

public void ConvertExternalToInternalLink(LinkItem linkItem)
{
    if (string.IsNullOrWhiteSpace(linkItem.Href) || linkItem.ContentLink != ContentReference.EmptyReference)
        return;

    var siteUrl = SiteDefinition.Current.SiteUrl;
    if (Uri.TryCreate(linkItem.Href, UriKind.Absolute, out var externalUri) 
        && externalUri.Host.Equals(siteUrl.Host, StringComparison.OrdinalIgnoreCase))
    {
        var relativePath = siteUrl.MakeRelativeUri(externalUri).ToString();
        var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
        
        if (contentLoader.TryGetByUrl(relativePath, out IContent matchingContent))
        {
            linkItem.ContentLink = matchingContent.ContentLink;
            linkItem.Href = null; // Let Episerver handle internal URL resolution
        }
    }
}

Note: Adjust this to handle edge cases like query strings, fragments, or non-existent pages based on your project’s needs.

2. Disable Automatic Conversion of External URLs to Internal Pages in LinkItemCollection

Episerver 7 automatically resolves external URLs pointing to your site into internal content references by default. To turn this off, you have two reliable options:

Option 1: Global Configuration via web.config

Add or update the linkResolver section in your web.config to disable automatic conversion globally:

<episerver.web>
  <linkResolver automaticExternalLinkConversion="false" />
</episerver.web>

This setting applies to all LinkItemCollection instances across your site.

Option 2: Granular Control with a Custom Editor Descriptor

If you only want to disable the behavior for specific properties, create a custom editor descriptor:

using EPiServer.Shell.ObjectEditing;
using EPiServer.Web.PropertyControls;

[EditorDescriptorRegistration(TargetType = typeof(LinkItemCollection))]
public class NoAutoConvertLinkEditorDescriptor : EditorDescriptor
{
    public override void ModifyMetadata(ExtendedMetadata metadata, IEnumerable<Attribute> attributes)
    {
        base.ModifyMetadata(metadata, attributes);
        
        var defaultEditor = metadata.EditorDescriptors.FirstOrDefault(e => e is LinkItemCollectionEditorDescriptor);
        if (defaultEditor != null)
        {
            var settings = defaultEditor.Settings as LinkItemCollectionEditorSettings;
            settings?.AllowAutomaticExternalLinkConversion = false;
        }
    }
}

To restrict this to a single property, add a UIHint to your content property and update the EditorDescriptorRegistration to target that hint.


内容的提问来源于stack exchange,提问作者Gonzalo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:51:20