Episerver 7中如何禁用LinkItemCollection外部URL转内部页面功能?
1. Convert External Links in LinkItem to Internal URLs
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
ContentLoaderto locate the corresponding content item using the URL's relative path. - Update the
LinkItemto use the internalContentLinkinstead of the externalHref.
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

