Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,13 @@ internal async Task InitializeStandardComponentServicesAsync(
routingStateProvider.RouteData = new RouteData(componentType, httpContext.GetRouteData().Values);
if (httpContext.GetEndpoint() is RouteEndpoint routeEndpoint)
{
routingStateProvider.RouteData.Template = routeEndpoint.RoutePattern.RawText;
// Verify that the endpoint route pattern matches the current component's route attributes
// to prevent stale route data during hot reload scenarios
if (IsValidRouteTemplateForComponent(componentType, routeEndpoint.RoutePattern.RawText))
{
routingStateProvider.RouteData.Template = routeEndpoint.RoutePattern.RawText;
}
// If template doesn't match, leave Template as null to force normal routing
}
}
}
Expand Down Expand Up @@ -253,6 +259,29 @@ private static string GetContextBaseUri(HttpRequest request)
return result.EndsWith('/') ? result : result += "/";
}

private static bool IsValidRouteTemplateForComponent(Type componentType, string? routeTemplate)
{
// If route template is null or empty, it's not valid
if (string.IsNullOrEmpty(routeTemplate))
{
return false;
}

// Get the current route attributes from the component type
var routeAttributes = componentType.GetCustomAttributes(typeof(RouteAttribute), inherit: false);

// Check if the endpoint's route template matches any of the component's current route attributes
foreach (RouteAttribute attribute in routeAttributes)
{
if (string.Equals(attribute.Template, routeTemplate, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}

private sealed class FormCollectionReadOnlyDictionary : IReadOnlyDictionary<string, StringValues>
{
private readonly IFormCollection _form;
Expand Down
Loading