diff --git a/src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs b/src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs index 3fe0814a153f..4c68d3428edb 100644 --- a/src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs +++ b/src/Umbraco.Web.Website/Routing/EagerMatcherPolicy.cs @@ -72,7 +72,8 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) } // If there's only one candidate, we don't need to do anything. - if (candidates.Count < 2) + var candidateCount = candidates.Count; + if (candidateCount < 2) { return; } @@ -85,6 +86,14 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) RouteEndpoint? dynamicEndpoint = null; for (var i = 0; i < candidates.Count; i++) { + if (candidates.IsValidCandidate(i) is false) + { + // If the candidate is not valid we reduce the candidate count so we can later ensure that there is always + // at least 1 candidate. + candidateCount -= 1; + continue; + } + CandidateState candidate = candidates[i]; // If it's not a RouteEndpoint there's not much we can do to count it in the order. @@ -123,7 +132,7 @@ public async Task ApplyAsync(HttpContext httpContext, CandidateSet candidates) // Invalidate the dynamic route if another route has a lower order. // This means that if you register your static route after the dynamic route, the dynamic route will take precedence // This more closely resembles the existing behaviour. - if (dynamicEndpoint is not null && dynamicId is not null && dynamicEndpoint.Order > lowestOrder) + if (dynamicEndpoint is not null && dynamicId is not null && dynamicEndpoint.Order > lowestOrder && candidateCount > 1) { candidates.SetValidity(dynamicId.Value, false); }