Take the community feedback survey now.
AI OnAI Off
Take the community feedback survey now.
If you don't need the PropertyUrl requirement, you can use a ContentReference instead and use an extension to get the simple url.
using EPiServer;
using EPiServer.Core;
using EPiServer.DataAnnotations;
using EPiServer.ServiceLocation;
using EPiServer.Web.Routing;
namespace ExampleExtensions
{
public static class ExampleContentExtensions
{
static Injected<IContentLoader> _ContentLoader;
public static string GetUrl(this ContentReference contentReference)
{
if (ContentReference.IsNullOrEmpty(contentReference))
return "#";
var content = _ContentLoader.Service.Get<IContent>(contentReference);
var externalProperty = content?.Property["PageExternalURL"];
if (!string.IsNullOrWhiteSpace(externalProperty?.ToString()))
{
return string.Format("/{0}/", externalProperty.ToString().Trim('/'));
}
return UrlResolver.Current.GetUrl(contentReference);
}
}
public class PageBase : PageData
{
[CultureSpecific]
[AllowedTypes(typeof(PageData))]
public virtual ContentReference LinkToContent { get; set; }
}
}
Thanks Brad,
I was looking for this kind of solution, but it was not working with PropertyUrl...
As I can see here they are using not only the Page references, but links to the catalogs (from EpiCommerce)... and I am not sure if they could reference here on other features of PropertyUrl... So I am looking the solution for PropertyUrl at the moment
I've not tested, but this may work for you. It should support Url, ContentReference, and IContent.
using EPiServer;
using EPiServer.Core;
using EPiServer.ServiceLocation;
using EPiServer.Web.Routing;
namespace ExampleExtensions
{
public static class ExampleContentExtensions
{
public static string GetUrl(this Url url)
{
var content = UrlResolver.Current.Route(new UrlBuilder(url));
return GetUrl(content);
}
public static string GetUrl(this ContentReference contentReference)
{
if (ContentReference.IsNullOrEmpty(contentReference)) return "#";
var content = ServiceLocator.Current.GetInstance<IContentLoader>().Get<IContent>(contentReference);
return GetUrl(content);
}
public static string GetUrl(this IContent content)
{
if (content == null) return "#";
var externalProperty = content?.Property["PageExternalURL"];
if (!string.IsNullOrWhiteSpace(externalProperty?.ToString()))
{
return string.Format("/{0}/", externalProperty.ToString().Trim('/'));
}
return UrlResolver.Current.GetUrl(content.ContentLink);
}
}
}
Thank you a lot, Brad!
I'll try this solution.
Upd: It works fine. Thanks!
Hi,
I am looking for some elegant solution for it if it exists. Thanks in advance