Skip to content

Bug: CurrentCategories only contains the first category when multiple categories are separated by / #54

Description

@improsina

We have identified an issue in Geta.Categories where CurrentCategories only contains the first category when multiple category segments are provided in the URL.

Example

Given a URL such as:/category1/category2/category3

Only the first category is resolved and added to CurrentCategories, while the remaining categories are ignored.

Suspected cause

The issue appears to originate from the following line in CategoryPartialRouter:

var remainingPath = nextSegment.Next.ToString().TrimEnd(_trailingSlash[0]);

This seems to process only the next segment rather than the full remaining path, causing category resolution to stop after the first category.

Proposed fix

Instead of using nextSegment.Next, the router should process the entire remaining path:
var remainingPath = segmentContext.RemainingSegments.TrimEnd(_trailingSlash).ToString();
The following implementation resolves the issue we have.

public object RoutePartial(ICategoryRoutableContent content, UrlResolverContext segmentContext)
{
    if (CategoriesResolved())
    {
        return null;
    }

    var remainingPath = segmentContext.RemainingSegments.TrimEnd(_trailingSlash).ToString();

    if (!string.IsNullOrWhiteSpace(remainingPath))
    {
        var localizableContent = content as ILocale;
        var preferredCulture = localizableContent?.Language ?? ContentLanguage.PreferredCulture;
        var segments = remainingPath.Split(new[] { CategorySeparator }, StringSplitOptions.RemoveEmptyEntries);

        var categoryUrlSegments = new List<string>();
        var categoriesFound = false;

        foreach (var segment in segments)
        {
            if (Configuration.UseUrlPathForCategoryRetrieval)
            {
                var category = CategoryLoader.GetCategoryByPath<CategoryData>(segment, preferredCulture);
                if (category != null)
                {
                    categoryUrlSegments.Add(segment);
                    categoriesFound = true;
                }
            }
            else
            {
                var urlSegment = segment.Contains(_trailingSlash)
                    ? segment.Split(_trailingSlash).Last()
                    : segment;

                var category = CategoryLoader.GetFirstBySegment<CategoryData>(urlSegment, preferredCulture);

                if (category != null)
                {
                    categoryUrlSegments.Add(urlSegment);
                    categoriesFound = true;
                }
            }
        }

        if (categoriesFound)
        {
            segmentContext.RemainingSegments = ReadOnlyMemory<char>.Empty;

            HttpContext.Request.RouteValues.Add(
                CategoryRoutingConstants.CurrentCategories,
                categoryUrlSegments.Distinct().ToArray());

            return content;
        }
    }

    return null;
}

Additional observation

When implementing the proposed fix, it is also necessary to clear the remaining segments after the categories have been resolved:
segmentContext.RemainingSegments = ReadOnlyMemory<char>.Empty;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions