Class RouteTemplateSelector
- Namespace
- Blazing.Mvvm.Components.Routing
- Assembly
- Blazing.Mvvm.dll
Selects the most appropriate route template from a collection based on provided navigation parameters.
public sealed class RouteTemplateSelectorInheritance
Remarks
This class implements the template selection algorithm that determines which route pattern best matches
the navigation intent when multiple @page directives are defined on a component.
Selection priority:
- No parameters provided ? primary route (simplest)
- Query string only ? primary route
- Path segments ? match by parameter count (exact match > optional parameters > catch-all)
Constructors
RouteTemplateSelector(ILogger<RouteTemplateSelector>)
Initializes a new instance of the RouteTemplateSelector class.
public RouteTemplateSelector(ILogger<RouteTemplateSelector> logger)Parameters
loggerILogger<RouteTemplateSelector>-
The logger for diagnostic output.
Methods
SelectBestTemplate(RouteTemplateCollection, string?)
Selects the best-matching route template from a collection based on the provided parameters.
public RouteTemplate SelectBestTemplate(RouteTemplateCollection templates, string? parameters)Parameters
templatesRouteTemplateCollection-
The collection of available route templates for the target ViewModel.
parametersstring?-
Optional navigation parameters that may include:
- Query string:
?key=value - Path segments:
value1/value2 - Combined:
value1/value2?key=value
- Query string:
Returns
- RouteTemplate
-
The selected RouteTemplate that best matches the navigation intent.
Examples
var selector = new RouteTemplateSelector(logger);
var collection = new RouteTemplateCollection
{
PrimaryRoute = "/test",
AllRoutes = new List<RouteTemplate>
{
new() { Pattern = "/test", Parameters = new() },
new() { Pattern = "/test/{echo}", Parameters = new() { new() { Name = "echo" } } }
}
};
// No parameters ? selects "/test"
var template1 = selector.SelectBestTemplate(collection, null);
// Query string only ? selects "/test"
var template2 = selector.SelectBestTemplate(collection, "?key=value");
// Path segment ? selects "/test/{echo}"
var template3 = selector.SelectBestTemplate(collection, "value123");
Remarks
Selection Algorithm:
1. No parameters or query string only: Returns the primary route.
2. Path segments provided: Analyzes the number of path segments and selects a template where:
- The template can accommodate the provided segment count
- Prefers exact match over optional parameters
- Prefers required parameters over optional
- Falls back to catch-all if available
3. No suitable match: Returns the primary route as a fallback.
Exceptions
- ArgumentNullException
-
Thrown when
templatesis null.