diff --git a/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs b/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs index b5c3d850d2..fe7c415add 100644 --- a/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs +++ b/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs @@ -35,6 +35,18 @@ namespace Umbraco.Web.PublishedCache return GetById(UmbracoContext.InPreviewMode, contentId); } + /// + /// Gets a content identified by its unique identifier. + /// + /// The content unique identifier. + /// The content, or null. + /// Considers published or unpublished content depending on context. + public IPublishedContent GetById(Guid contentId) + { + var intId = UmbracoContext.Application.Services.EntityService.GetIdForKey(contentId, UmbracoObjectTypes.Document); + return GetById(intId.Success ? intId.Result : -1); + } + /// /// Gets a content identified by its unique identifier. /// diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs index 05b6b0e468..ee2745bbb2 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs @@ -23,7 +23,9 @@ using RenderingEngine = Umbraco.Core.RenderingEngine; namespace Umbraco.Web.Routing { - internal class PublishedContentRequestEngine + using Core.Models; + + internal class PublishedContentRequestEngine { private readonly PublishedContentRequest _pcr; private readonly RoutingContext _routingContext; @@ -518,7 +520,9 @@ namespace Umbraco.Web.Routing const string tracePrefix = "FollowInternalRedirects: "; if (_pcr.PublishedContent == null) - throw new InvalidOperationException("There is no PublishedContent."); + { + throw new InvalidOperationException("There is no PublishedContent."); + } bool redirect = false; var internalRedirect = _pcr.PublishedContent.GetPropertyValue(Constants.Conventions.Content.InternalRedirectId); @@ -527,37 +531,51 @@ namespace Umbraco.Web.Routing { ProfilingLogger.Logger.Debug("{0}Found umbracoInternalRedirectId={1}", () => tracePrefix, () => internalRedirect); - int internalRedirectId; - if (int.TryParse(internalRedirect, out internalRedirectId) == false) - internalRedirectId = -1; + IPublishedContent internalRedirectNode = null; + var udiInternalRedirectIdAttempt = internalRedirect.TryConvertTo(); + var intInternalRedirectIdAttempt = internalRedirect.TryConvertTo(); + + if (udiInternalRedirectIdAttempt.Success) + { + // Try and get the redirect node from a UDI ID + internalRedirectNode = + _routingContext.UmbracoContext.ContentCache.GetById(udiInternalRedirectIdAttempt.Result.Guid); + } + else if (intInternalRedirectIdAttempt.Success) + { + // Try and get the redirect node from a legacy integer ID + internalRedirectNode = + _routingContext.UmbracoContext.ContentCache.GetById(intInternalRedirectIdAttempt.Result); + } + else + { + // bad redirect - log and display the current page (legacy behavior) + ProfilingLogger + .Logger.Debug("{0}Failed to redirect to id={1}: invalid value", + () => tracePrefix, () => internalRedirect); + } - if (internalRedirectId <= 0) - { - // bad redirect - log and display the current page (legacy behavior) - //_pcr.Document = null; // no! that would be to force a 404 - ProfilingLogger.Logger.Debug("{0}Failed to redirect to id={1}: invalid value", () => tracePrefix, () => internalRedirect); - } - else if (internalRedirectId == _pcr.PublishedContent.Id) - { - // redirect to self - ProfilingLogger.Logger.Debug("{0}Redirecting to self, ignore", () => tracePrefix); - } - else - { - // redirect to another page - var node = _routingContext.UmbracoContext.ContentCache.GetById(internalRedirectId); - - if (node != null) - { - _pcr.SetInternalRedirectPublishedContent(node); // don't use .PublishedContent here - redirect = true; - ProfilingLogger.Logger.Debug("{0}Redirecting to id={1}", () => tracePrefix, () => internalRedirectId); - } - else - { - ProfilingLogger.Logger.Debug("{0}Failed to redirect to id={1}: no such published document", () => tracePrefix, () => internalRedirectId); - } - } + if (internalRedirectNode == null) + { + ProfilingLogger + .Logger + .Debug("{0}Failed to redirect to id={1}: no such published document", + () => tracePrefix, () => internalRedirect); + } + else if (internalRedirectNode.Id == _pcr.PublishedContent.Id) + { + // redirect to self + ProfilingLogger.Logger.Debug("{0}Redirecting to self, ignore", + () => tracePrefix); + } + else + { + // Redirect to another page + _pcr.SetInternalRedirectPublishedContent(internalRedirectNode); + redirect = true; + ProfilingLogger.Logger.Debug("{0}Redirecting to id={1}", () => tracePrefix, + () => internalRedirect); + } } return redirect;