Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

Adds a new WithStaticAssets extension method that works with any IEndpointConventionBuilder, enabling endpoints that return RazorComponentResult and other custom endpoints to attach ResourceCollection metadata.

Problem

Previously, the WithStaticAssets method was only available on RazorComponentsEndpointConventionBuilder, limiting its use to Blazor component endpoints. This created a gap for scenarios where developers wanted to use RazorComponentResult in regular endpoints (like MapGet, MapPost, etc.) but still needed the same static asset metadata that Blazor component endpoints receive automatically.

Solution

This PR introduces EndpointConventionBuilderResourceCollectionExtensions.WithStaticAssets<TBuilder>() that:

  • Extends any IEndpointConventionBuilder to attach ResourceCollection metadata
  • Takes an IEndpointRouteBuilder parameter to resolve static assets from
  • Supports optional manifest path parameter for custom manifests
  • Automatically prevents duplicate metadata by checking for existing ResourceAssetCollection
  • Uses the same underlying infrastructure (ResourceCollectionResolver) as the existing Blazor-specific implementation

Usage

var app = WebApplication.CreateBuilder().Build();
app.MapStaticAssets();

// Now any endpoint can have static asset metadata
app.MapGet("/page/{name}", (string name) => 
   new RazorComponentResult<MyComponent>(new { Name = name }))
   .WithStaticAssets(app);

app.MapPost("/api/render", (RenderRequest request) => 
   new RazorComponentResult<DynamicComponent>(request.Parameters))
   .WithStaticAssets(app, "custom-manifest.json");

Implementation Details

The new extension method:

  1. Creates a ResourceCollectionResolver from the provided IEndpointRouteBuilder
  2. Adds a convention that checks for existing metadata to avoid duplicates
  3. Resolves and attaches the same metadata types as the Blazor version: ResourceAssetCollection, ResourcePreloadCollection, and ImportMapDefinition
  4. Only applies metadata when static assets are actually registered for the specified manifest

This maintains full compatibility with existing code while extending the functionality to work with any endpoint convention builder.

Fixes #58937

Original prompt

This section details on the original issue you should resolve

<issue_title>[Blazor] Provide a helper to attach ResourceCollection metadata to other endpoints</issue_title>
<issue_description>This will be useful in the context of RazorComponentResult

#58937 (comment)</issue_description>

<agent_instructions>We want to provide a helper like that doesn't use RazorComponentsEndpointConventionBuilder but instead just extends IEndpointRouteBuilder

public static RazorComponentsEndpointConventionBuilder WithStaticAssets(
this RazorComponentsEndpointConventionBuilder builder,
string? manifestPath = null)
{
ArgumentNullException.ThrowIfNull(builder);
builder.ManifestPath = manifestPath;
if (!builder.ResourceCollectionConventionRegistered)
{
builder.ResourceCollectionConventionRegistered = true;
var convention = new ResourceCollectionConvention(new ResourceCollectionResolver(builder.EndpointRouteBuilder));
builder.BeforeCreateEndpoints += convention.OnBeforeCreateEndpoints;
builder.Add(convention.ApplyConvention);
}
return builder;

It can be simplified to only do the following. If not done, it should check there isn't a resource collection already on the metadata.

            var convention = new ResourceCollectionConvention(new ResourceCollectionResolver(builder.EndpointRouteBuilder));
            builder.Add(convention.ApplyConvention);
```</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

Fixes #63398

💡 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.

@Copilot Copilot AI changed the title [WIP] [Blazor] Provide a helper to attach ResourceCollection metadata to other endpoints [Blazor] Add WithStaticAssets extension for IEndpointConventionBuilder Sep 25, 2025
@Copilot Copilot AI requested a review from javiercn September 25, 2025 11:55
Copilot finished work on behalf of javiercn September 25, 2025 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Blazor] Provide a helper to attach ResourceCollection metadata to other endpoints MapStaticAssets and Minimal APIs not working as expected
2 participants