Esempio n. 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(PublishedRequest request)
        {
            if (request.PublishedContent == null)
            {
                throw new InvalidOperationException("There is no PublishedContent.");
            }

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

            var internalRedirectId = request.PublishedContent.Value(Constants.Conventions.Content.InternalRedirectId)?.ToString();

            if (internalRedirectId == null)
            {
                // no value stored, just return, no need to log
                return(false);
            }

            if (int.TryParse(internalRedirectId, out var internalRedirectIdAsInt) && internalRedirectIdAsInt == request.PublishedContent.Id)
            {
                // redirect to self
                _logger.Debug <PublishedRouter>("FollowInternalRedirects: Redirecting to self, ignore");
                return(false);
            }

            IPublishedContent internalRedirectNode = null;

            if (internalRedirectIdAsInt > 0)
            {
                // try and get the redirect node from a legacy integer ID
                internalRedirectNode = request.UmbracoContext.Content.GetById(internalRedirectIdAsInt);
            }
            else if (GuidUdi.TryParse(internalRedirectId, out var internalRedirectIdAsUdi))
            {
                // try and get the redirect node from a UDI Guid
                internalRedirectNode = request.UmbracoContext.Content.GetById(internalRedirectIdAsUdi.Guid);
            }

            if (internalRedirectNode == null)
            {
                _logger.Debug <PublishedRouter, object>("FollowInternalRedirects: Failed to redirect to id={InternalRedirectId}: no such published document.",
                                                        request.PublishedContent.GetProperty(Constants.Conventions.Content.InternalRedirectId).GetSourceValue());
                return(false);
            }

            request.SetInternalRedirectPublishedContent(internalRedirectNode); // don't use .PublishedContent here
            _logger.Debug <PublishedRouter, int>("FollowInternalRedirects: Redirecting to id={InternalRedirectId}", internalRedirectIdAsInt);
            return(true);
        }
Esempio n. 2
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(PublishedRequest request)
        {
            if (request.PublishedContent == null)
            {
                throw new InvalidOperationException("There is no PublishedContent.");
            }

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

            var redirect = false;
            var valid    = false;
            IPublishedContent internalRedirectNode = null;
            var internalRedirectId = request.PublishedContent.Value(Constants.Conventions.Content.InternalRedirectId, defaultValue: -1);

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

            if (valid == false)
            {
                // bad redirect - log and display the current page (legacy behavior)
                _logger.Debug <PublishedRouter>("FollowInternalRedirects: Failed to redirect to id={InternalRedirectId}: value is not an int nor a GuidUdi.",
                                                request.PublishedContent.GetProperty(Constants.Conventions.Content.InternalRedirectId).GetSourceValue());
            }

            if (internalRedirectNode == null)
            {
                _logger.Debug <PublishedRouter>("FollowInternalRedirects: Failed to redirect to id={InternalRedirectId}: no such published document.",
                                                request.PublishedContent.GetProperty(Constants.Conventions.Content.InternalRedirectId).GetSourceValue());
            }
            else if (internalRedirectId == request.PublishedContent.Id)
            {
                // redirect to self
                _logger.Debug <PublishedRouter>("FollowInternalRedirects: Redirecting to self, ignore");
            }
            else
            {
                request.SetInternalRedirectPublishedContent(internalRedirectNode); // don't use .PublishedContent here
                redirect = true;
                _logger.Debug <PublishedRouter>("FollowInternalRedirects: Redirecting to id={InternalRedirectId}", internalRedirectId);
            }

            return(redirect);
        }