Skip to content

Support binding parameters to type Uri in Minimal Actions #36649

Closed
@martincostello

Description

@martincostello

Is your feature request related to a problem? Please describe.

In MVC controllers actions, it is possible to strongly-type a parameter expected to be a URI as Uri. For example:

public IActionResult MyAction([FromQuery(Name = "redirect_uri")] Uri? redirectUri)
{
    // Implementation
}

However, attempting to replicate this with a Minimal Action endpoint results in an exception being thrown.

app.MapGet("/my-action", ([FromQuery(Name = "redirect_uri")] Uri? redirectUri) =>
{
    // Implementation
});
  Message: 
System.InvalidOperationException : No public static bool Uri.TryParse(string, out Uri) method found for redirectUri.

  Stack Trace: 
RequestDelegateFactory.BindParameterFromValue(ParameterInfo parameter, Expression valueExpression, FactoryContext factoryContext)
RequestDelegateFactory.CreateArgument(ParameterInfo parameter, FactoryContext factoryContext)
RequestDelegateFactory.CreateArguments(ParameterInfo[] parameters, FactoryContext factoryContext)
RequestDelegateFactory.CreateTargetableRequestDelegate(MethodInfo methodInfo, RequestDelegateFactoryOptions options, FactoryContext factoryContext, Expression targetExpression)
RequestDelegateFactory.Create(Delegate handler, RequestDelegateFactoryOptions options)
DelegateEndpointRouteBuilderExtensions.Map(IEndpointRouteBuilder endpoints, RoutePattern pattern, Delegate handler)
DelegateEndpointRouteBuilderExtensions.MapMethods(IEndpointRouteBuilder endpoints, String pattern, IEnumerable`1 httpMethods, Delegate handler)
DelegateEndpointRouteBuilderExtensions.MapGet(IEndpointRouteBuilder endpoints, String pattern, Delegate handler)

Describe the solution you'd like

Either of:

  1. Minimal Actions (RequestDelegateFactory) special-cases Uri to bind parameters;
  2. Minimal Actions supports extending model binding for types not owned by the application where a TryParse or BindAsync method cannot be added;
  3. The Uri type adds a TryParse() method that can be consumed by the built-in binding.

Additional context

Found using .NET SDK 6.0.100-rc.1.21458.32 to port an existing MVC application to use Minimal Actions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:3Work that is nice to havearea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routinghelp wantedUp for grabs. We would accept a PR to help resolve this issueold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labelstriage-focusAdd this label to flag the issue for focus at triage

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions