November Happy Hour will be moved to Thursday December 5th.

SimpleAddress from PropertyUrl

Vote:
 

Hi,

  1.  I have certain page having both long (Page1/Page2/MyPage) and simple (MyPage) addresses.
  2.  Then I want to reference it in certain place via PropertyUrl:
            [CultureSpecific]
            [Required]
            [BackingType(typeof(PropertyUrl))]
            [Display(
                Name = "Link",
                Description = "Link to the page",
                GroupName = SystemTabNames.Content,
                Order = 1)]
            public virtual Url Link { get; set; }
  3.  I want the simple address (if it exists) to be used for the routing or url rendering but not the long one.

I am looking for some elegant solution for it if it exists. Thanks in advance

#174314
Jan 24, 2017 12:07
Vote:
 

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; }
    }
}
#174324
Jan 24, 2017 14:59
Vote:
 

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

#174327
Edited, Jan 24, 2017 15:12
Vote:
 

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);
        }
    }
}
#174333
Jan 24, 2017 15:44
Vote:
 

Thank you a lot, Brad!

I'll try this solution.

Upd: It works fine. Thanks!

#174334
Edited, Jan 24, 2017 15:56
Vote:
 

Still could not mark Brad's post as an Answer... Image

#174379
Edited, Jan 25, 2017 10:56
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.