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 <nikolajlauridsen@protonmail.ch>
This commit is contained in:
Bjarke Berg
2024-11-12 11:52:07 +01:00
committed by GitHub
parent 04a064a287
commit e38f6e8967
2 changed files with 8 additions and 7 deletions

View File

@@ -441,7 +441,7 @@ public class DocumentUrlService : IDocumentUrlService
return "#"; 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 "#"; return "#";
} }

View File

@@ -20,7 +20,7 @@ namespace Umbraco.Cms.Infrastructure.Routing
private readonly IPublishedContentCache _contentCache; private readonly IPublishedContentCache _contentCache;
private readonly IDocumentNavigationQueryService _navigationQueryService; private readonly IDocumentNavigationQueryService _navigationQueryService;
private readonly ILogger<RedirectTracker> _logger; private readonly ILogger<RedirectTracker> _logger;
private readonly IDocumentUrlService _documentUrlService; private readonly IPublishedUrlProvider _publishedUrlProvider;
public RedirectTracker( public RedirectTracker(
IVariationContextAccessor variationContextAccessor, IVariationContextAccessor variationContextAccessor,
@@ -29,7 +29,7 @@ namespace Umbraco.Cms.Infrastructure.Routing
IPublishedContentCache contentCache, IPublishedContentCache contentCache,
IDocumentNavigationQueryService navigationQueryService, IDocumentNavigationQueryService navigationQueryService,
ILogger<RedirectTracker> logger, ILogger<RedirectTracker> logger,
IDocumentUrlService documentUrlService) IPublishedUrlProvider publishedUrlProvider)
{ {
_variationContextAccessor = variationContextAccessor; _variationContextAccessor = variationContextAccessor;
_localizationService = localizationService; _localizationService = localizationService;
@@ -37,7 +37,7 @@ namespace Umbraco.Cms.Infrastructure.Routing
_contentCache = contentCache; _contentCache = contentCache;
_navigationQueryService = navigationQueryService; _navigationQueryService = navigationQueryService;
_logger = logger; _logger = logger;
_documentUrlService = documentUrlService; _publishedUrlProvider = publishedUrlProvider;
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -64,7 +64,8 @@ namespace Umbraco.Cms.Infrastructure.Routing
{ {
try try
{ {
var route = _contentCache.GetRouteById(publishedContent.Id, culture); var route = _publishedUrlProvider.GetUrl(publishedContent.Id, UrlMode.Relative, culture).TrimEnd(Constants.CharArrays.ForwardSlash);
if (IsValidRoute(route)) if (IsValidRoute(route))
{ {
oldRoutes[(publishedContent.Id, culture)] = (publishedContent.Key, 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. // Retry using all languages, if this is invariant but has a variant ancestor.
foreach (string languageIsoCode in languageIsoCodes.Value) 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)) if (IsValidRoute(route))
{ {
oldRoutes[(publishedContent.Id, languageIsoCode)] = (publishedContent.Key, route); oldRoutes[(publishedContent.Id, languageIsoCode)] = (publishedContent.Key, route);
@@ -102,7 +103,7 @@ namespace Umbraco.Cms.Infrastructure.Routing
{ {
try 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) if (!IsValidRoute(newRoute) || oldRoute == newRoute)
{ {
continue; continue;