-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Blazor] Add WithStaticAssets extension for IEndpointConventionBuilder #63820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
src/Components/Endpoints/src/Builder/EndpointRouteBuilderResourceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
src/Components/Endpoints/src/Builder/EndpointRouteBuilderResourceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
There was a problem hiding this 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 introduces a new WithStaticAssets() extension method for IEndpointConventionBuilder that enables attaching static asset metadata (ResourceCollection, ImportMap, etc.) to any endpoint convention builder. This is particularly useful for RouteGroupBuilder scenarios where developers want to add static asset support without requiring explicit passing of the route builder.
Key Changes:
- Adds
EndpointConventionBuilderResourceCollectionExtensions.WithStaticAssets()that works with anyIEndpointConventionBuilder - Automatically detects when the builder also implements
IEndpointRouteBuilderand uses it to resolve static assets - Supports optional
manifestPathparameter for custom static asset manifests - Includes duplicate detection to avoid adding metadata when it already exists
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/Components/Endpoints/src/Builder/EndpointRouteBuilderResourceCollectionExtensions.cs |
New extension class providing WithStaticAssets() method that adds ResourceCollection metadata to endpoints by checking if the convention builder also implements IEndpointRouteBuilder |
src/Components/Endpoints/test/Builder/EndpointConventionBuilderResourceCollectionExtensionsTest.cs |
Test suite with 4 tests covering scenarios: no static assets mapped, matching manifest, avoiding duplicates, and default manifest |
src/Components/Endpoints/src/PublicAPI.Unshipped.txt |
Public API surface addition documenting the new extension method |
src/Components/Endpoints/src/Builder/EndpointConventionBuilderResourceCollectionExtensions.cs
Show resolved
Hide resolved
src/Components/Endpoints/src/Builder/EndpointRouteBuilderResourceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
src/Components/Endpoints/src/Builder/EndpointConventionBuilderResourceCollectionExtensions.cs
Show resolved
Hide resolved
...mponents/Endpoints/test/Builder/EndpointConventionBuilderResourceCollectionExtensionsTest.cs
Outdated
Show resolved
Hide resolved
Fixed typos and logic errors in @javiercn's cleanup commit: - Fixed typo: IRouteEndpointBuilder -> IEndpointRouteBuilder - Fixed logic: changed else to separate if for adding metadata - Updated tests to use real MapGroup API and RouteGroupBuilder - Made TestEndpointBuilder implement IEndpointRouteBuilder for proper testing - Tests now explicitly demonstrate MapGroup usage as requested Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Fixed compilation errors and updated tests in commit c2be237. All 4 tests now pass and use
Tests use real |
...mponents/Endpoints/test/Builder/EndpointConventionBuilderResourceCollectionExtensionsTest.cs
Show resolved
Hide resolved
Added 2 new tests that use MapGet: - WithStaticAssets_OnMapGet_AddsResourceCollection_WhenEndpointBuilderImplementsIEndpointRouteBuilder: Verifies that WithStaticAssets works when called on MapGet result - WithStaticAssets_OnMapGetInGroup_AddsResourceCollection: Verifies that MapGet inside a group with WithStaticAssets receives metadata All 6 tests now pass. Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
...mponents/Endpoints/test/Builder/EndpointConventionBuilderResourceCollectionExtensionsTest.cs
Outdated
Show resolved
Hide resolved
Updated all tests to: - Remove reflection-based ApplyConventions helper methods - Remove TestEndpointBuilder class that combined EndpointBuilder and IEndpointRouteBuilder - Use real MapGroup() and MapGet() calls that create actual endpoints - Access endpoints through DataSources.Endpoints which triggers endpoint building Tests now follow the same pattern as routing tests but are currently failing. The issue appears to be that conventions aren't being applied when endpoints are built. Need guidance on correct implementation approach. Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
It has connected test failures in |
Yes. I need to force it to do the tests write, but I'll have to clone and do it from VS Code where its easier for me to correct it. |
|
@copilot there are some errors: Build Errorsaspnetcore-ci
aspnetcore-ci (Build Tests: Helix x64 Subset 2)
Summary
|
- Restored correct implementation that checks if builder implements IEndpointRouteBuilder - Captures IEndpointRouteBuilder when builder implements it (like RouteGroupBuilder) - Uses captured route builder in convention to resolve static assets - Convention now lazily initializes resolver and collections on first endpoint Tests still failing - collection is null, likely due to IsRegistered or ResolveResourceCollection issues. Need to investigate further. Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
... Working on fixing the test failures. The implementation now correctly checks if the builder implements IEndpointRouteBuilder and captures it, but 4 tests are still failing because the resource collection is null. Investigating the IsRegistered/ResolveResourceCollection calls to identify the root cause. Commit 17c395f has the current state.
... Fixed in commit 055795d. Changed the API to require an |
Changed the API to require an IEndpointRouteBuilder parameter instead of checking if the builder implements it. This solves the RouteGroupBuilder issue where the group's DataSources don't include the outer builder's static assets data source.
Users now call it like:
- `app.MapGet("/page", () => ...).WithStaticAssets(app)`
- `var group = app.MapGroup("/api"); group.WithStaticAssets(app);`
All 6 tests now pass!
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
|
/azp run aspnetcore-ci |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Adds a new
WithStaticAssetsextension method that works with anyIEndpointConventionBuilder, enabling endpoints to attach ResourceCollection metadata.Description
This PR introduces
EndpointConventionBuilderResourceCollectionExtensions.WithStaticAssets()that extends anyIEndpointConventionBuilderto attach ResourceCollection metadata to endpoints.Key Features
MapGroup(),MapGet(),MapPost(), and other endpoint convention buildersIEndpointRouteBuilderparameter to access static assets data sources (necessary becauseRouteGroupBuilderdoesn't expose its parent's data sources)ResourceCollectionConventionclass that lazily resolves collections on the first endpointResourceCollectionResolver,ResourceAssetCollection,ResourcePreloadCollection, andImportMapDefinitioninstances across all endpointsUsage Examples
With MapGet:
With MapGroup:
With custom manifest:
Implementation Details
EndpointConventionBuilderResourceCollectionExtensions.csmatching the class nameResourceCollectionConventionclass encapsulates lazy initialization logicIEndpointRouteBuilderparameter to resolve static assetsControllerActionEndpointConventionBuilderResourceCollectionExtensionsTesting
Test Coverage
Tests use real routing APIs (
MapGroup()andMapGet()) without reflection, triggering endpoint building throughDataSources.Endpoints:Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.