示例#1
0
        public static string GetSimpleAddressIfExits(this PageReference pageLink)
        {
            if (PageReference.IsNullOrEmpty(pageLink))
            {
                return(string.Empty);
            }

            var page = pageLink.GetPage();

            if (page == null)
            {
                return(string.Empty);
            }
            var    simpleaddress = page.GetPropertyValue("PageExternalURL");
            string pageUrl;

            if (!string.IsNullOrEmpty(simpleaddress))
            {
                pageUrl = string.Format("/{0}/", simpleaddress);
            }
            else
            {
                var urlResolver = ServiceLocator.Current.GetInstance <UrlResolver>();
                pageUrl = urlResolver.GetUrl(pageLink);
            }
            return(pageUrl);
        }
示例#2
0
        /// <summary>
        /// Gets the canonical URL for a page. If LinkType is FetchData the original page URL will be the canonical link.
        /// </summary>
        /// <param name="pageLink">The PageReference to get canonical URL from.</param>
        /// <param name="considerFetchDataFrom">Consider fetch data from setting in EPiServer.</param>
        /// <returns></returns>
        public static string GetCanonicalUrl(this PageReference pageLink, bool considerFetchDataFrom = true)
        {
            if (PageReference.IsNullOrEmpty(pageLink))
            {
                return(null);
            }

            PageData page = pageLink.GetPage();

            if (page == null)
            {
                return(null);
            }

            return(page.GetCanonicalUrl(considerFetchDataFrom));
        }
        /// <summary>
        /// Gets the top page reference, i e page beneath the start page, starting at the specified page reference
        /// </summary>
        public static PageReference GetTopPage(this PageReference pageLink)
        {
            if (PageReference.IsNullOrEmpty(pageLink))
            {
                throw new NotSupportedException("Current top page cannot be retrieved without a starting point, and the specified page link was empty");
            }

            var page = pageLink.GetPage();

            while (!PageReference.IsNullOrEmpty(page.ParentLink) && !page.ParentLink.CompareToIgnoreWorkID(PageReference.RootPage) && !page.ParentLink.CompareToIgnoreWorkID(PageReference.StartPage))
            {
                page = page.ParentLink.GetPage();
            }

            return(page.PageLink);
        }
        /// <summary>
        /// Gets the canonical link for a page.
        /// </summary>
        /// <param name="page">Page to get canonical url for</param>
        /// <param name="considerFetchDataFrom">Consider fetch data from setting in EPiServer.</param>
        /// <param name="considerSimpleAddress">Use simple address of page if it is set.</param>
        /// <param name="urlResolver">Optional UrlResolver instance.</param>
        /// <returns>The complete link to the page. If LinkType is FetchData then the original page URL will be returned.</returns>
        public static string GetCanonicalUrl(this PageData page, bool considerFetchDataFrom = true, bool considerSimpleAddress = false, UrlResolver urlResolver = null)
        {
            if (page == null)
            {
                return(null);
            }

            PageData      canonicalPage = page;
            PageReference pageLink      = page.PageLink;

            if (considerFetchDataFrom && page.LinkType == PageShortcutType.FetchData)
            {
                var shortcutLink = page["PageShortcutLink"] as PageReference;

                if (!PageReference.IsNullOrEmpty(shortcutLink))
                {
                    pageLink = shortcutLink;
                }
            }

            if (!page.PageLink.CompareToIgnoreWorkID(canonicalPage.PageLink))
            {
                canonicalPage = pageLink.GetPage();
            }

            if (considerSimpleAddress)
            {
                var simpleAddress = canonicalPage["PageExternalUrl"] as string;

                if (!string.IsNullOrWhiteSpace(simpleAddress))
                {
                    return(simpleAddress.GetExternalUrl());
                }
            }

            return(canonicalPage.PageLink.GetFriendlyUrl(true, urlResolver));
        }