Skip to content

Conversation

@javiercn
Copy link
Member

Summary

When computing endpoints for reference static web assets, the base path prefix check was using string.StartsWith which incorrectly matched routes like App1.styles.css against base path App1. This caused the base path to not be prepended, resulting in missing routes.

Problem

The bug occurs in ComputeEndpointsForReferenceStaticWebAssets when determining if a route already has the base path applied. The original check:

if (oldRoute.StartsWith(asset.BasePath, StringComparison.Ordinal))

Would incorrectly match:

  • Route App1.styles.css with base path App1incorrectly skipped (starts with "App1")
  • Route App1.lib.module.js with base path App1incorrectly skipped

This resulted in these files being served without the proper base path prefix in multi-project Blazor scenarios.

Solution

Introduced StaticWebAssetEndpoint.RouteHasPathPrefix method that uses PathTokenizer to compare paths segment-by-segment, ensuring only proper path segment boundaries are matched:

Route Prefix Result Reason
App1/css/app.css App1 ✅ true Proper segment match
App1.styles.css App1 ❌ false Not a segment boundary
App1Bundle/app.js App1 ❌ false Different segment
App1 App1 ✅ true Exact match

The comparison is case-insensitive and handles both / and \ path separators.

Testing

  • Added unit tests for RouteHasPathPrefix covering positive and negative matches
  • Added regression tests in ComputeEndpointsForReferenceStaticWebAssetsTest
  • Validated E2E with multi-project Blazor WebAssembly app (App1, App2, AppHost)

Files Changed

  • ComputeEndpointsForReferenceStaticWebAssets.cs - Use new segment-based prefix check
  • StaticWebAssetEndpoint.cs - Add RouteHasPathPrefix helper method
  • StaticWebAssetEndpointTest.cs - Unit tests for RouteHasPathPrefix
  • ComputeEndpointsForReferenceStaticWebAssetsTest.cs - Regression tests

…parison

When computing endpoints for reference static web assets, the base path
prefix check was using string.StartsWith which incorrectly matched routes
like 'App1.styles.css' against base path 'App1'. This caused the base path
to not be prepended, resulting in missing routes like 'App1/App1.styles.css'.

The fix introduces RouteHasPathPrefix method that uses PathTokenizer to
compare paths segment-by-segment, ensuring only proper path segment boundaries
are matched. The comparison is case-insensitive.

Example:
- Route 'App1/css/app.css' has prefix 'App1' -> true (segment match)
- Route 'App1.styles.css' has prefix 'App1' -> false (not a segment boundary)
- Route 'App1Bundle/app.js' has prefix 'App1' -> false (different segment)
Copilot AI review requested due to automatic review settings December 23, 2025 22:17
@github-actions github-actions bot added the Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch label Dec 23, 2025
@javiercn javiercn requested a review from maraf December 23, 2025 22:17
@dotnet-policy-service
Copy link
Contributor

Thanks for your PR, @@javiercn.
To learn about the PR process and branching schedule of this repo, please take a look at the SDK PR Guide.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug in static web assets base path prefix detection where string.StartsWith incorrectly matched routes like App1.styles.css against base path App1, causing missing route prefixes in multi-project Blazor scenarios. The solution introduces segment-based path comparison using PathTokenizer to ensure only proper path segment boundaries are matched.

Key Changes:

  • Introduced StaticWebAssetEndpoint.RouteHasPathPrefix for segment-based path prefix comparison
  • Updated ComputeEndpointsForReferenceStaticWebAssets to use the new method instead of string.StartsWith
  • Added comprehensive unit tests and regression tests

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/StaticWebAssetsSdk/Tasks/Data/StaticWebAssetEndpoint.cs Added RouteHasPathPrefix static method for segment-based path prefix comparison using PathTokenizer
src/StaticWebAssetsSdk/Tasks/ComputeEndpointsForReferenceStaticWebAssets.cs Replaced string.StartsWith with RouteHasPathPrefix and added segment list reuse for performance
test/Microsoft.NET.Sdk.StaticWebAssets.Tests/StaticWebAssets/StaticWebAssetEndpointTest.cs New test file with comprehensive unit tests for RouteHasPathPrefix covering positive/negative matches and edge cases
test/Microsoft.NET.Sdk.StaticWebAssets.Tests/StaticWebAssets/ComputeEndpointsForReferenceStaticWebAssetsTest.cs Added regression tests validating the bug fix and correct base path application behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-AspNetCore RazorSDK, BlazorWebAssemblySDK, dotnet-watch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants