Beispiel #1
0
        /// <summary>
        /// Follows internal redirections through the <c>umbracoInternalRedirectId</c> document property.
        /// </summary>
        /// <returns>A value indicating whether redirection took place and led to a new published document.</returns>
        /// <remarks>
        /// <para>Redirecting to a different site root and/or culture will not pick the new site root nor the new culture.</para>
        /// <para>As per legacy, if the redirect does not work, we just ignore it.</para>
        /// </remarks>
        private bool FollowInternalRedirects()
        {
            const string tracePrefix = "FollowInternalRedirects: ";

            if (_pcr.PublishedContent == null)
            {
                throw new InvalidOperationException("There is no PublishedContent.");
            }

            bool redirect         = false;
            var  internalRedirect = _pcr.PublishedContent.GetPropertyValue <string>(Constants.Conventions.Content.InternalRedirectId);

            if (!string.IsNullOrWhiteSpace(internalRedirect))
            {
                LogHelper.Debug <PublishedContentRequestEngine>("{0}Found umbracoInternalRedirectId={1}", () => tracePrefix, () => internalRedirect);

                int internalRedirectId;
                if (!int.TryParse(internalRedirect, out internalRedirectId))
                {
                    internalRedirectId = -1;
                }

                if (internalRedirectId <= 0)
                {
                    // bad redirect - log and display the current page (legacy behavior)
                    //_pcr.Document = null; // no! that would be to force a 404
                    LogHelper.Debug <PublishedContentRequestEngine>("{0}Failed to redirect to id={1}: invalid value", () => tracePrefix, () => internalRedirect);
                }
                else if (internalRedirectId == _pcr.PublishedContent.Id)
                {
                    // redirect to self
                    LogHelper.Debug <PublishedContentRequestEngine>("{0}Redirecting to self, ignore", () => tracePrefix);
                }
                else
                {
                    // redirect to another page
                    var node = _routingContext.UmbracoContext.ContentCache.GetById(internalRedirectId);

                    _pcr.SetInternalRedirectPublishedContent(node); // don't use .PublishedContent here
                    if (node != null)
                    {
                        redirect = true;
                        LogHelper.Debug <PublishedContentRequestEngine>("{0}Redirecting to id={1}", () => tracePrefix, () => internalRedirectId);
                    }
                    else
                    {
                        LogHelper.Debug <PublishedContentRequestEngine>("{0}Failed to redirect to id={1}: no such published document", () => tracePrefix, () => internalRedirectId);
                    }
                }
            }

            return(redirect);
        }
        /// <summary>
        /// Follows internal redirections through the <c>umbracoInternalRedirectId</c> document property.
        /// </summary>
        /// <returns>A value indicating whether redirection took place and led to a new published document.</returns>
        /// <remarks>
        /// <para>Redirecting to a different site root and/or culture will not pick the new site root nor the new culture.</para>
        /// <para>As per legacy, if the redirect does not work, we just ignore it.</para>
        /// </remarks>
        private bool FollowInternalRedirects()
        {
            const string tracePrefix = "FollowInternalRedirects: ";

            if (_pcr.PublishedContent == null)
            {
                throw new InvalidOperationException("There is no PublishedContent.");
            }

            // don't try to find a redirect if the property doesn't exist
            if (_pcr.PublishedContent.HasProperty(Constants.Conventions.Content.InternalRedirectId) == false)
            {
                return(false);
            }

            var redirect = false;
            IPublishedContent internalRedirectNode = null;
            var internalRedirectId =
                _pcr.PublishedContent.GetPropertyValue <int>(Constants.Conventions.Content.InternalRedirectId, -1);
            var valueValid = false;

            if (internalRedirectId > 0)
            {
                valueValid = true;
                // Try and get the redirect node from a legacy integer ID
                internalRedirectNode = _routingContext.UmbracoContext.ContentCache.GetById(internalRedirectId);
            }
            else
            {
                var udiInternalRedirectId =
                    _pcr.PublishedContent.GetPropertyValue <GuidUdi>(Constants.Conventions.Content.InternalRedirectId);
                if (udiInternalRedirectId != null)
                {
                    valueValid = true;
                    // Try and get the redirect node from a UDI Guid
                    internalRedirectNode =
                        _routingContext.UmbracoContext.ContentCache.GetById(udiInternalRedirectId.Guid);
                }
            }

            if (valueValid == false)
            {
                // bad redirect - log and display the current page (legacy behavior)
                ProfilingLogger
                .Logger.Debug <PublishedContentRequestEngine>(
                    "{0}Failed to redirect, value of '{1}' is not an int nor a GuidUdi",
                    () => tracePrefix, () => Constants.Conventions.Content.InternalRedirectId);
            }

            if (internalRedirectNode == null)
            {
                ProfilingLogger.Logger.Debug <PublishedContentRequestEngine>(
                    "{0}Failed to redirect, value of '{1}' does not lead to a published document", () => tracePrefix,
                    () => Constants.Conventions.Content.InternalRedirectId);
            }
            else if (internalRedirectNode.Id == _pcr.PublishedContent.Id)
            {
                // redirect to self
                ProfilingLogger.Logger.Debug <PublishedContentRequestEngine>("{0}Redirecting to self, ignore",
                                                                             () => tracePrefix);
            }
            else
            {
                // Redirect to another page
                _pcr.SetInternalRedirectPublishedContent(internalRedirectNode);
                redirect = true;
                ProfilingLogger.Logger.Debug <PublishedContentRequestEngine>("{0}Redirecting to id={1}", () => tracePrefix,
                                                                             () => internalRedirectNode.Id);
            }

            return(redirect);
        }