Skip to content

Add Map implementation with overload for InferMetadata and CreateRequestDelegate #46163

@captainsafia

Description

@captainsafia

Background and Motivation

For the purposes of the request delegate generator, we want to be able to invoke the logic in the RouteEndpointDataSource but provide a custom implementation of a Map method that will allow the user to pass custom delegates for inferring metadata and producing a request delegate. The source generator will call this overload with implementations generated at compile time to circumvent calling APIs that use runtime code generation.

Proposed API

public static class GeneratedRouteEndpointBuilderExtensions
{
  public static Microsoft.AspNetCore.Builder.RouteHandlerBuilder Map(
    this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder routes,
    string pattern,
    System.Delegate handler,
    IEnumerable<string> httpMethods,
    Func<MethodInfo, RequestDelegateFactoryOptions?, RequestDelegateMetadataResult> populateMetadata,
    Func<Delegate, RequestDelegateFactoryOptions, RequestDelegateMetadataResult?, RequestDelegateResult> createRequestDelegate)
  {
    return routes
      .GetOrAddRouteEndpointDataSource()
      .AddRouteHandler(RoutePatternFactory.Parse(pattern),
                       handler,
                       httpMethods,
                       isFallback: false,
                       populateMetadata,
                       createRequestDelegate);
}

Usage Examples

[GeneratedCode]
internal static Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapPut(
  this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
  [System.Diagnostics.CodeAnalysis.StringSyntax("Route")] string pattern, 
  Delegate handler)
{
  var populateMetadata = (methodInfo, options) => {
    var metadata = new List<object>();
    // ...
    return new RequestDelegateMetadataResult { EndpointMetadata = AsReadOnlyList(metadata) };
  };
  var createRequestDelegate = (del, options, requestDelegateMetadataResult) => {
    // ...
    return new RequestDelegateResult(finalRequestDelegate, requestDelegateMetadataResult.Metadata);
  };
  return Map(endpoints, pattern, handler, GetVerb, populateMetadata, createRequestDelegate);
}

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-rdgold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions