From e38f6e89670f4c07f0224cfb86c21ffd3f830069 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Tue, 12 Nov 2024 11:52:07 +0100 Subject: [PATCH] Fixes to redirect management (#17491) * Fixed issue with redirects due to culture being empty string when null was expected. Furtermore, using IPublishedUrlProvider instead of DocumentUrlService directly. to ensure you can use your own routing stuff * Trim trailing slash when saving redirect --------- Co-authored-by: nikolajlauridsen --- src/Umbraco.Core/Services/DocumentUrlService.cs | 2 +- .../Routing/RedirectTracker.cs | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Core/Services/DocumentUrlService.cs b/src/Umbraco.Core/Services/DocumentUrlService.cs index 7548ae9cb7..69686e5b94 100644 --- a/src/Umbraco.Core/Services/DocumentUrlService.cs +++ b/src/Umbraco.Core/Services/DocumentUrlService.cs @@ -441,7 +441,7 @@ public class DocumentUrlService : IDocumentUrlService return "#"; } - if(isDraft is false && culture != null && _publishStatusQueryService.IsDocumentPublished(documentKey, culture) is false) + if(isDraft is false && string.IsNullOrWhiteSpace(culture) is false && _publishStatusQueryService.IsDocumentPublished(documentKey, culture) is false) { return "#"; } diff --git a/src/Umbraco.Infrastructure/Routing/RedirectTracker.cs b/src/Umbraco.Infrastructure/Routing/RedirectTracker.cs index ee752af798..e26fa7d35c 100644 --- a/src/Umbraco.Infrastructure/Routing/RedirectTracker.cs +++ b/src/Umbraco.Infrastructure/Routing/RedirectTracker.cs @@ -20,7 +20,7 @@ namespace Umbraco.Cms.Infrastructure.Routing private readonly IPublishedContentCache _contentCache; private readonly IDocumentNavigationQueryService _navigationQueryService; private readonly ILogger _logger; - private readonly IDocumentUrlService _documentUrlService; + private readonly IPublishedUrlProvider _publishedUrlProvider; public RedirectTracker( IVariationContextAccessor variationContextAccessor, @@ -29,7 +29,7 @@ namespace Umbraco.Cms.Infrastructure.Routing IPublishedContentCache contentCache, IDocumentNavigationQueryService navigationQueryService, ILogger logger, - IDocumentUrlService documentUrlService) + IPublishedUrlProvider publishedUrlProvider) { _variationContextAccessor = variationContextAccessor; _localizationService = localizationService; @@ -37,7 +37,7 @@ namespace Umbraco.Cms.Infrastructure.Routing _contentCache = contentCache; _navigationQueryService = navigationQueryService; _logger = logger; - _documentUrlService = documentUrlService; + _publishedUrlProvider = publishedUrlProvider; } /// @@ -64,7 +64,8 @@ namespace Umbraco.Cms.Infrastructure.Routing { try { - var route = _contentCache.GetRouteById(publishedContent.Id, culture); + var route = _publishedUrlProvider.GetUrl(publishedContent.Id, UrlMode.Relative, culture).TrimEnd(Constants.CharArrays.ForwardSlash); + if (IsValidRoute(route)) { oldRoutes[(publishedContent.Id, culture)] = (publishedContent.Key, route); @@ -74,7 +75,7 @@ namespace Umbraco.Cms.Infrastructure.Routing // Retry using all languages, if this is invariant but has a variant ancestor. foreach (string languageIsoCode in languageIsoCodes.Value) { - route = _contentCache.GetRouteById(publishedContent.Id, languageIsoCode); + route = _publishedUrlProvider.GetUrl(publishedContent.Id, UrlMode.Relative, languageIsoCode).TrimEnd(Constants.CharArrays.ForwardSlash); if (IsValidRoute(route)) { oldRoutes[(publishedContent.Id, languageIsoCode)] = (publishedContent.Key, route); @@ -102,7 +103,7 @@ namespace Umbraco.Cms.Infrastructure.Routing { try { - var newRoute = _documentUrlService.GetLegacyRouteFormat(contentKey, culture, false); + var newRoute = _publishedUrlProvider.GetUrl(contentKey, UrlMode.Relative, culture).TrimEnd(Constants.CharArrays.ForwardSlash); if (!IsValidRoute(newRoute) || oldRoute == newRoute) { continue;