Skip to content

RequestDelegateFactory should take an optional set of route pattern names or route parameter names #33700

Closed
@davidfowl

Description

@davidfowl

Background and Motivation

This would allow unattributed methods to disambiguate between route and query string when the method parameters are unattributed. Today we generate code that prefers route over query string and we could avoid that in certain cases. It would also allow us to better fail if the user used [FromRoute] and there was no route parameter with that name defined.

Proposed API

Since our overloads are getting a bit crazy with this change, I suggest we add an options object to handle the overflow.

public static class RequestDelegateFactory
{
-    public static RequestDelegate Create(Delegate action, IServiceProvider? serviceProvider)
+    public static RequestDelegate Create(Delegate action, Func<HttpContext, object>? targetFactory = null, RequestDelegateFactoryOptions? options = null)
-    public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, object>? targetFactory = null, IServiceProvider? serviceProvider)
+    public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, object>? targetFactory = null, RequestDelegateFactoryOptions? options = null)
}

+ public class RequestDelegateFactoryOptions
+ {
+     public IServiceProvider? ServiceProvider { get; init; }
+     public IReadOnlyList<string>? RouteParameterNames { get; init; }
+ }

Usage Examples

var options = new RequestDelegateFactoryOptions 
{ 
    ServiceProvider  = serviceProvider, 
    RouteParameterNames  = new[] { "id" }
};

var rd = RequestDelegateFactory.Create((int id) => id, options: options);

Alternative Designs

A single static method with optional parameters.

public static class RequestDelegateFactory
{
+    public static RequestDelegate Create(MethodInfo methodInfo, Func<HttpContext, object>? targetFactory = null, IServiceProvider? serviceProvider = null, IReadOnlyList<string>? routeParameterNames = null);
}

Risks

None

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-minimal-actionsController-like actions for endpoint routing

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions